/    Sign up×
Community /Pin to ProfileBookmark

preg_match.. why no matches?

I have some code:

[code=php]<?php

preg_match (“/[0-9]{1, 2}/”, ’24horses’, $matches);
var_dump($matches);

?>[/code]

$matches is empty… why isn’t it matching the ’24’ in ’24horses’?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@callumdauthorAug 24.2007 — Found the problem.

The space in the range {1, 2}..

(why didn't PHP throw up an error?)
Copy linkTweet thisAlerts:
@NogDogAug 24.2007 — ...

(why didn't PHP throw up an error?)[/QUOTE]

Probably because, as stated in the [url=http://www.php.net/manual/en/reference.pcre.pattern.syntax.php]preg syntax page[/url]:
An opening curly bracket that appears in a position where a quantifier is not allowed, or one that does not match the syntax of a quantifier, is taken as a literal character. For example, {,6} is not a quantifier, but a literal string of four characters.[/quote]
So I suspect it was just treated as a literal string rather than a repetition specifier.
Copy linkTweet thisAlerts:
@callumdauthorAug 27.2007 — Oh, right.

Thanks NogDog.
×

Success!

Help @callumd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...