homepage Welcome to WebmasterWorld Guest from 54.234.225.23
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...
irubin




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

Hello,

I have the following strings:

52{1}2
52
525{11}2

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

Any ideas?

-R

 

MonkeeSage




msg:1277683
 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().

Jordan

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

coopster




msg:1277684
 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>";
}

returns:

Array
(
[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,
\w+(?=;)
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