Page 1 of 1

regex solution ?

Posted: Wed Apr 22, 2009 11:11 pm
by PHPycho
How to check/match the following case using regex:

Code: Select all

$string = 'test #any_word';
//other cases:
//$string = 'test test #any_word';
//$string = 'test test test #any_word';
//$string = 'test test test ... #any_word';
 
if(preg_match('/pattern goes here.../', $string){
    echo 'Matched';
}else{
    echo 'Not Matched';
}

Re: regex solution ?

Posted: Wed Apr 22, 2009 11:59 pm
by prometheuzz
PHPycho wrote:How to check/match the following case using regex:

Code: Select all

$string = 'test #any_word';
//other cases:
//$string = 'test test #any_word';
//$string = 'test test test #any_word';
//$string = 'test test test ... #any_word';
 
if(preg_match('/pattern goes here.../', $string){
    echo 'Matched';
}else{
    echo 'Not Matched';
}
If you could explain what you're looking for, then I can tell you he pattern. So, what is the significance? Every word has an underscore in it, is that it? Or the word 'test'? Or a hash (#)? Or because the sentence consists of more than one word?

Re: regex solution ?

Posted: Thu Apr 23, 2009 1:09 am
by PHPycho
I have got the solution as:

pattern becomes:

Code: Select all

/^(test )+\#([a-z]+)/i

Re: regex solution ?

Posted: Thu Apr 23, 2009 6:30 am
by PHPycho
is there any alternatives ??