View Full Version : regex syntax help
11-13-2006, 02:04 PM
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 :
sir p :D
11-13-2006, 03:31 PM
Yeah, the pipe means or in a regex...escape character is the \...
11-13-2006, 03: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..
$data = "abc|123";
the 123 is ignored but if i do ..
$data = "abc123|";
it works fine...
11-13-2006, 03:44 PM
What is your php code?
11-13-2006, 03:46 PM
Use preg_match_all() instead of preg_match()
11-13-2006, 03: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..
many thanks for your time both of you, much apperciated.
ralph l mayo
11-13-2006, 07: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
# First part
preg_match('/\A(.*?)[|]/xms', $subject, $match);
# Last part
preg_match('/[|](.*)\z/xms', $subject, $match);
# Position of the separator
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.
11-27-2006, 12: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
11-27-2006, 12: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);
11-27-2006, 04:47 PM
fantastic.. explode.. so simple.. brilliant!
thanks very much
11-28-2006, 06:01 AM
But yea, explode is a cleaner solution - regex isn't always the answer ;)
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.