homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Quick Regex Question
help is appreciated...

 12:34 am on Sep 18, 2003 (gmt 0)


I have the following strings:


I need to extract everything up to the first { (if it exists)

Any ideas?




 12:39 am on Sep 18, 2003 (gmt 0)

I think the pattern should be...


...if there is no "{" it will get the whole string, if there is it will get everything up to it.

Not sure about the PHP method, but I think it is preg_match().


[edit:] Oops, shouldn't have included the grouping parentheticals [/edit]


 1:02 am on Sep 18, 2003 (gmt 0)

Are you looking for digits only before the brace? And only once? If so:

$string = '52{1}2';
if (preg_match("/\d+(?={)/", $string, $matches)) {
print "<pre>"; print_r($matches); print "</pre>";


[0] => 52

because (right out of the manual) [us2.php.net]:
"An assertion subpattern is matched in the normal way, except that it does not cause the current matching position to be changed. Lookahead assertions start with (?= for positive assertions and (?! for negative assertions. For example,
matches a word followed by a semicolon, but does not include the semicolon in the match..."

If you want to match more than once within a single string, you will need to use preg_match_all() and add a subpattern to the regular expression. Let us know if this is the case. Regards, Coop

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved