regular expressions
Moderator: General Moderators
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
regular expressions
does anyone know what the regular expression for the following are:
1) a string that contains only the digits 0-9, can contain only 7 digits (no more, no less) and cannot contain spaces
2) a stirng that contains only characters a-z, both uppercase and lowercase and can contain spaces. ie. it is a stirng used for storing a name
1) a string that contains only the digits 0-9, can contain only 7 digits (no more, no less) and cannot contain spaces
2) a stirng that contains only characters a-z, both uppercase and lowercase and can contain spaces. ie. it is a stirng used for storing a name
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
preg_match('#^ї0-9]{7}$#', $text1);
preg_match('#^їA-Z ]$#i', $text2);1) a string that contains only the digits 0-9, can contain only 7 digits (no more, no less) and cannot contain spaces
#^[0-9]{7}$#
#^[a-zA-Z ]$#2) a stirng that contains only characters a-z, both uppercase and lowercase and can contain spaces. ie. it is a stirng used for storing a name
Warning I'm a regex noob. So test these.
Feyd will probably answer this post before I ever hit submit...
Is that right Feyd?
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
hey feyd
thanks for reply
1st reg expression runs ok
2nd reg expression (to identify names, ie upper and lowercase letters only, spaces allowed) doesnt seem to be working. this example has your regular expression, see what happens when you run it:
<?
$name = "Danny";
if (!preg_match('#^[a-zA-Z]$#i', $name))
echo "invalid";
else
echo "valid";
?>
thanks for reply
1st reg expression runs ok
2nd reg expression (to identify names, ie upper and lowercase letters only, spaces allowed) doesnt seem to be working. this example has your regular expression, see what happens when you run it:
<?
$name = "Danny";
if (!preg_match('#^[a-zA-Z]$#i', $name))
echo "invalid";
else
echo "valid";
?>
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
oops.. my brain was off..
Code: Select all
#^їA-Z ]+$#-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Just add a backslash floowed by a single quote in the square brackets... dude, all you're doing is specifying the permitted characters 
Code: Select all
#^їA-Z'' ]+$#i