Welcome to WebmasterWorld Guest from 54.159.250.110

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)

10+ Year Member



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 Sep 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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 Sep 18, 2003 (gmt 0)

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



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