Page 1 of 1
help with a regular expression
Posted: Wed Feb 01, 2006 12:07 pm
by jasongr
Hi people
I need help with a simple regular expression in PHP
I have a variable called $ch with holds a letter
I am given a string $test and I need to know if the string has the following format:
<the letter><any number><whitespace><other characters>
Assuming $ch is 'B', here are example:
B123 aafafda
B4 afdf
B9 afffdad
any help would be appreciated
Posted: Wed Feb 01, 2006 12:24 pm
by pickle
Moved to Regex forum
This one might work:
Edit: There should be an 'i' after the closing delimiter to make sure the pattern is case-insensitive:
Posted: Wed Feb 01, 2006 12:28 pm
by Burrito
Code: Select all
<?php
$ch = "B9 afffdad";
preg_match("/(\w{1}).*?/",$ch,$matches);
echo $matches[1];
?>
Posted: Wed Feb 01, 2006 1:08 pm
by Chris Corbyn
Note.... the {1} is kinda redundant since just \w or [a-z] by itself implies {1}
EDIT | Redundant was a bad choice of words.... let's say "superfluous" instead

Posted: Wed Feb 01, 2006 2:11 pm
by pickle
Burrito wrote:Code: Select all
<?php
$ch = "B9 afffdad";
preg_match("/(\w{1}).*?/",$ch,$matches);
echo $matches[1];
?>
Will this work? Won't it also match "9asdf" and "_99we"?
Posted: Wed Feb 01, 2006 2:14 pm
by Burrito
strip out the first letter that will which what I thought s/he wanted it is based on this:
Assuming $ch is 'B', here are example:
B123 aafafda
B4 afdf
B9 afffdad
Posted: Wed Feb 01, 2006 3:24 pm
by raghavan20
Untested...
Code: Select all
$pattern = '#?-i('.$ch.'){1}\d+\s.*#i';
preg_match_all($pattern, $input, $matches);
print_r($matches);
Re: help with a regular expression
Posted: Wed Feb 01, 2006 3:41 pm
by yakasha
jasongr wrote:Hi people
I need help with a simple regular expression in PHP
I have a variable called $ch with holds a letter
I am given a string $test and I need to know if the string has the following format:
<the letter><any number><whitespace><other characters>
Assuming $ch is 'B', here are example:
B123 aafafda
B4 afdf
B9 afffdad
any help would be appreciated
Code: Select all
preg_match( "%^{$ch}\d+\s+[A-Za-z]+$%i", $test );
Posted: Wed Feb 01, 2006 4:02 pm
by raghavan20
Revised and tested...this matches any sequence with the format
<one letter><atleast one digit><one space><any character atleast once>
Code: Select all
<pre>
<?php
$input = "bB23432 sadfdsa";
$input1 = "B23432 sadfdsa";
$input2 = "C23432 sadfdsa";
$input3 = "B3 fd324#@$@#";
$ch = "B";
$pattern = '#\b('.$ch.'){1}\d+\s.+#';
echo "<br />bB23432 sadfdsa --> ".preg_match($pattern, $input);
echo "<br />B23432 sadfdsa --> ".preg_match($pattern, $input1);
echo "<br />C23432 sadfdsa --> ".preg_match($pattern, $input2);
echo "<br />B3 fd324#@$@# --> ".preg_match($pattern, $input3);
?>
</pre>
output:
Code: Select all
bB23432 sadfdsa --> 0
B23432 sadfdsa --> 1
C23432 sadfdsa --> 0
B3 fd324#@$@# --> 1
But unfortunately this does not work... I am trying to use what I learnt from d11wtq's crash course part 2...
Code: Select all
$pattern = '#\b(?-i'.$ch.'){1}\d+\s.+#i';
Posted: Thu Feb 02, 2006 3:44 am
by jasongr
thanks for the helpe people
I am using yakasha's code which seems to do the trick