regex solution ?

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

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

regex solution ?

Post 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';
}
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regex solution ?

Post 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?
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: regex solution ?

Post by PHPycho »

I have got the solution as:

pattern becomes:

Code: Select all

/^(test )+\#([a-z]+)/i
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: regex solution ?

Post by PHPycho »

is there any alternatives ??
Post Reply