help with a regular expression

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

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

help with a regular expression

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Moved to Regex forum

This one might work:

Code: Select all

/[a-z]{1}\d*?\s*?\w*/
Edit: There should be an 'i' after the closing delimiter to make sure the pattern is case-insensitive:

Code: Select all

/[a-z]{1}\d*?\s*?\w*/i
Last edited by pickle on Wed Feb 01, 2006 2:33 pm, edited 1 time in total.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

<?php
$ch = "B9 afffdad";
preg_match("/(\w{1}).*?/",$ch,$matches);
echo $matches[1];
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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"?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Untested...

Code: Select all

$pattern = '#?-i('.$ch.'){1}\d+\s.*#i';
preg_match_all($pattern, $input, $matches);
print_r($matches);
Last edited by raghavan20 on Wed Feb 01, 2006 4:03 pm, edited 1 time in total.
yakasha
Forum Newbie
Posts: 10
Joined: Wed Aug 03, 2005 1:17 pm
Location: Las Vegas, NV
Contact:

Re: help with a regular expression

Post 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 );
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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';
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

thanks for the helpe people
I am using yakasha's code which seems to do the trick
Post Reply