View Full Version : regex syntax help

sir pannels
11-13-2006, 03:04 PM
hi all,

doing some regex pattern searching, i'm trying to search for the pipe character...

but the pattern im using does not pick it up, do i need to escape it somehow?

the pattern im using is :


any thoughts?
sir p :D

11-13-2006, 04:31 PM
Yeah, the pipe means or in a regex...escape character is the \...

Good luck;

sir pannels
11-13-2006, 04:36 PM
thanks Brandoe.. cheers

something that has cropped up from that, it seems when the pipe is in my text string, anything that follows a pipe is ignored..

var was:
$data = "abc|123";
the 123 is ignored but if i do ..
$data = "abc123|";
it works fine...

any thoughts?
P :D

11-13-2006, 04:44 PM
What is your php code?

11-13-2006, 04:46 PM
Use preg_match_all() instead of preg_match()

sir pannels
11-13-2006, 04:52 PM
here it is:

$subject = "abcd|ef";
$pattern = '/^\|/';
preg_match($pattern, substr($subject,1), $matches, PREG_OFFSET_CAPTURE);

Hi Mar... I used all, just changed the above to _all and it completed the script but did not fill the array, seems it didnt pick up the pipe..

any pointers?
many thanks for your time both of you, much apperciated.

ralph l mayo
11-13-2006, 08:37 PM
What are you trying to do here? That regex matches any string with a pipe as the first character but doesn't save a match at all (no parens).

$subject = "abcd|ef";

# Both parts
print_r(explode('|', $subject));

# First part
preg_match('/\A(.*?)[|]/xms', $subject, $match);

# Last part
preg_match('/[|](.*)\z/xms', $subject, $match);

# Position of the separator
print_r(strpos($subject, '|'));

Generally, if you have pipe separated data to deal with it's time to refactor your data structure to use something more portable and less error-prone, but YMMV.

sir pannels
11-27-2006, 01:26 PM
Ralph thanks for your reply. Sorry for my delayed responce.

In answer to your question, all I'm trying to do is extract everything after a pipe if the pipe is there.. is there is no pipe, nothing happens at all....

I will play with your code now. I hope my answer provides more info
cheers for that

Sir P

11-27-2006, 01:57 PM
You dont need to use regular expression to extract everyting after the|char, you can use explode() which also is faster in this case.



$string = "Exctract everything|after the pipe char";
list(, $var) = explode('|', $string, 2);

echo $var;


sir pannels
11-27-2006, 05:47 PM
fantastic.. explode.. so simple.. brilliant!
thanks very much

11-28-2006, 07:01 AM
echo $post_pipe[1];

But yea, explode is a cleaner solution - regex isn't always the answer ;)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum