By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

preg_match frustration

P: n/a
Can someone help a regex noob?

my code:

$text = "clapham junction 1989";
$regexp = "[0-9][0-9]{3}";
$dummy = preg_match($regexp, $text, $matches);
echo $matches[0];

produces the error:

Warning: preg_match() [function.preg-match]: Unknown modifier '[' in
blah blah blah

Have I got the regex wrong? I'm trying to return any 4 digit number in
$text.

thanks in advance

Garry
Sep 21 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
..oO(GazK)
>Can someone help a regex noob?

my code:

$text = "clapham junction 1989";
$regexp = "[0-9][0-9]{3}";
$dummy = preg_match($regexp, $text, $matches);
echo $matches[0];

produces the error:

Warning: preg_match() [function.preg-match]: Unknown modifier '[' in
blah blah blah
You have to use delimiters around your regex, e.g.

$regexp = '/[0-9][0-9]{3}/';

See the manual for syntax details.
>Have I got the regex wrong? I'm trying to return any 4 digit number in
$text.
Shorter:

/\d{4}/

Micha
Sep 21 '08 #2

P: n/a
Michael Fesser wrote:
.oO(GazK)
>Can someone help a regex noob?

my code:

$text = "clapham junction 1989";
$regexp = "[0-9][0-9]{3}";
$dummy = preg_match($regexp, $text, $matches);
echo $matches[0];

produces the error:

Warning: preg_match() [function.preg-match]: Unknown modifier '[' in
blah blah blah

You have to use delimiters around your regex, e.g.

$regexp = '/[0-9][0-9]{3}/';

See the manual for syntax details.
>Have I got the regex wrong? I'm trying to return any 4 digit number in
$text.

Shorter:

/\d{4}/

Micha
That's great Micha, thanks very much.
Sep 21 '08 #3

P: n/a
..oO(GazK)
>That's great Micha, thanks very much.
You're welcome.

Micha
Sep 21 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.