whats the regex for this expression ??

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

whats the regex for this expression ??

Post by PHPycho »

what will be the regex for the string that contains only alphanumeric ,commma ,space .
Thanks in advance to all of you
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

$regex = '/^[a-z0-9, ]+$/i';
Note: an empty string won't match this pattern. If you want to allow empty strings too: change the '+' into '*'.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Remember to trim() that sucker afterward. ;)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Well indeed, that's a good idea :)
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

whats the regex for newline.
thank you
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

PHPycho wrote:whats the regex for newline.
What do you mean by that?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

PHPycho wrote:whats the regex for newline.
\n, just like always (it's got to be enclosed in double-quotes if you use the code above)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

aaronhall wrote:\n, just like always (it's got to be enclosed in double-quotes if you use the code above)
No, it doesn't.

Code: Select all

[feyd@home]>php -r "$a = '#\n#'; echo preg_match($a,chr(0x0A));"
1
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I'm still not sure what you mean, but try this:

Code: Select all

$regex = '/^[a-z0-9, \n\r]+$/i';
P.S Single quotes seem to work too. \n alone doesn't work for me while \n\r does.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Oren wrote:I'm still not sure what you mean, but try this:

Code: Select all

$regex = '/^[a-z0-9, \n\r]+$/i';
P.S Single quotes seem to work too. \n alone doesn't work for me while \n\r does.
Which is why PHP_EOS should be used for cross-platform applications :wink:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

feyd wrote:
aaronhall wrote:\n, just like always (it's got to be enclosed in double-quotes if you use the code above)
No, it doesn't.

Code: Select all

[feyd@home]>php -r "$a = '#\n#'; echo preg_match($a,chr(0x0A));"
1
Indeed, thanks
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Jcart wrote:Which is why PHP_EOS should be used for cross-platform applications :wink:
You mean PHP_EOL :wink:
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Knock Knock
Help again..
Whats the regex for the format like
883 02 12313545
or
883-02-12313545
thanks in advance for help
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

aaronhall wrote:
PHPycho wrote:whats the regex for newline.
\n, just like always (it's got to be enclosed in double-quotes if you use the code above)
\n is actually part of the pattern so it doesn't. Hexadec such as \x0A work in single quotes in patterns too.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

we seem to be turning into a regex factory instead of a regex classroom...

play here: http://www.cuneytyilmaz.com/prog/jrx/

we're here if you get stuck
Post Reply