Jump to content

regex fails with optional 'the' ([Tt][Hh][Ee] )


rubing

Recommended Posts

howdy pardners,

 

i got some trouble here...

 

I am trying to match input from an email message of the form;  'show place' and then extract the value of place.  So, I wrote this little snippet of code, which does the job well!

 

$pat='[sS][Hh][Oo][Ww] [[:alnum:]._\'\"&]{1,150}';
ereg($pat, $email, $query_word);
$message_body =$query_word[0];

$nopat=' [[:alnum:]._\'\"&]{1,150}'; 
ereg($nopat, $message_body, $query_final);
$message_final =$query_final[0];

 

However, now I realize that users will sometimes enter: 'show the place'  (e.g. show the rockies), in which case my regex miserably fails  >:(

 

I tried to rewriting my pattern as follows, but now it won't match anything.  :-\

 

 

$pat='[sS][Hh][Oo][Ww] [([Tt][Hh][Ee]  )[:alnum:]._\'\"&]{1,150}';

Link to comment
https://forums.phpfreaks.com/topic/98638-regex-fails-with-optional-the-tthhee/
Share on other sites

allrighty i figured out that

 

$pat='[sS][Hh][Oo][Ww] (([Tt][Hh][Ee] )[[:alnum:]._\'\"&]{1,150}|[[:alnum:]._\'\"&]{1,150})';

 

will pick up either 'show place' or 'show the place'

 

now i just got to figure out how to exctract just the place for either case!  8)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.