help with a regular expression
Moderator: General Moderators
help with a regular expression
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
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
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:
This one might work:
Code: Select all
/[a-z]{1}\d*?\s*?\w*/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.
Code: Select all
<?php
$ch = "B9 afffdad";
preg_match("/(\w{1}).*?/",$ch,$matches);
echo $matches[1];
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Will this work? Won't it also match "9asdf" and "_99we"?Burrito wrote:Code: Select all
<?php $ch = "B9 afffdad"; preg_match("/(\w{1}).*?/",$ch,$matches); echo $matches[1]; ?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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.
Re: help with a regular expression
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 );- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Revised and tested...this matches any sequence with the format
<one letter><atleast one digit><one space><any character atleast once>
output:
But unfortunately this does not work... I am trying to use what I learnt from d11wtq's crash course part 2...
<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>Code: Select all
bB23432 sadfdsa --> 0
B23432 sadfdsa --> 1
C23432 sadfdsa --> 0
B3 fd324#@$@# --> 1Code: Select all
$pattern = '#\b(?-i'.$ch.'){1}\d+\s.+#i';