Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Quick Regex Question

help is appreciated...

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

Junior Member

10+ Year Member

joined:Jan 1, 2003
posts:53
votes: 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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 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]

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 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
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members