Delimiter is in the string!

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

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Delimiter is in the string!

Post by spacebiscuit »

Hi,

I am trying to extract the date from a string with this regular expression:

Code: Select all

/\d{2}/\d{1,2}/\d{4}/
The problem is that part of the string is the forward slash '/' which is also the string delimiter (?). How do I search on this charcter without causing the error:

Unknown modifier '\'

Thanks in advance.....

Rob.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

I'm no expert on Regex but I think part of your problem is this:

Code: Select all

/d{2}/d{1,2}/d{4}/
should be

Code: Select all

\d{2}\d{1,2}\d{4}
as "\d" is any single digit and \d{2} would be two digits.

You just have the wrong slashes.

How are you applying this to your string though? You may be implementing it slightly wrong as well.

Edit: agh! Stupid PHPBB stripped the backslashes from my original code example! (fixed by putting it in Code tags instead of PHP)
Last edited by Skittlewidth on Wed Dec 14, 2005 9:35 am, edited 3 times in total.
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

From "Regex CRASH Course! (Pt. 1)"
viewtopic.php?t=33147
To delimit a regex we start and end with the EXACT same character. The two standards are (but you can use most non-alphanumeric characters):
Code:

/pattern/
#pattern#
Post Reply