Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

escaping slashes in preg_match

Status
Not open for further replies.

dbrb2

Instructor
Joined
Jul 19, 2004
Messages
121
Location
GB
Hello,

I'm having some trouble with my regular expressions...
I want to match a date using preg match, where the delimiter may be one of either " ", "/","-","." or "\"
Code:
$dateMatch = '/^\s*([0-9]{1,2})[//// \\\.-]+([0-9]{1,2})[//// \\\.-]*([0-9]{0,4})/';
  
preg_match($dateMatch,$str,$match);
I tried the above patten, with no joy. I think the problem lies with my escaping of forward and back slashes... any ideas?
 
Hi

Use backslash ( \ ) to escape the slashes ( / ) :
Code:
$dateMatch = '/^\s*([0-9]{1,2})[[COLOR=red pink]\[/color]/ \\.-]+([0-9]{1,2})[[COLOR=red pink]\[/color]/ \\.-]*([0-9]{0,4})/';
Or just use a different delimiter for the regular expression :
Code:
$dateMatch = '[COLOR=red pink]#[/color]^\s*([0-9]{1,2})[/ \\.-]+([0-9]{1,2})[/ \\.-]*([0-9]{0,4})[COLOR=red pink]#[/color]';

Feherke.
 
but there are (much) better ways to match a date... for example checkdate() (separate the date parts with split or explode). or use strototime() (or any other method).
 
Hi

Hmm... I thought that "date" was a typo and dbrb2 wanted to write "data".

Such regular expression would match for example [tt]' 0-----0 '[/tt] too.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top