473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

preg_match frustration

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
3 1396
..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
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
..oO(GazK)
>That's great Micha, thanks very much.
You're welcome.

Micha
Sep 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&" preg_match_all ("|product=(\d{5,10})&|i", $html, $out); $out...
0
by: awebguynow | last post by:
I ran across this code, and it kind of made me nervous: (as an email validator) if ( !preg_match("/.*\@.*\..*/", $_POST) | preg_match("/(\)/", $_POST) ) 1) from bitwise experience with "C",...
4
by: squash | last post by:
I have a string equal to 'www/' that I want to use in a preg_match. Php keeps giving me the warning: Warning: preg_match(): Unknown modifier '/' How can I escape the string so the / in www/ is...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
13
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this:...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.