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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Help

Status
Not open for further replies.

joelwenzel

Programmer
Jun 28, 2002
448
Can anyone tell me why this regular expression will not match.

var myRegExp = new RegExp("SelectedDate=\d{1,2}/\d{1,2}/\d{4}");

It is supposed to match SelectedDate=5/3/2002 or SelectedDate=12/12/2002
 
First of all, are you using Pattern and Matcher?
Regardless, to place \'s inside a String, remember that you have to use '\\'. Otherwise its trys to insert an escape character, like tab (\t).
Try "SelectedDate=\\d{1,2}/\\d{1,2}/\\d{4}"

 
You don't need to escape the "\" character since it is used for the digits, but you will need to escape the "/". Make it "\/". --Derek

"Fear not the storm for this is where we grow strong."
 
For the java.util.regex package, you DO need to use \\ as \ in String literals, since \d signifies a digit and the only way to put \ in a literal is with \\ (or unicode). You do NOT need to use \/ instead of /.
The following code is correct:
Code:
String s1 = "blahblahSelectedDate=5/3/2002blah";
String s2 = "blahSelectedDate=12/12/2002blahblah";

Pattern p = Pattern.compile("SelectedDate=\\d{1,2}/\\d{1,2}/\\d{4}");
Matcher m = p.matcher(s1);
if (m.find())
{
  System.out.println("s1: Found " + m.group());
}
m = p.matcher(s2);
if (m.find())
{
  System.out.println("s2: Found " + m.group());
}
-HavaTheJut
 
My bad I guess...its backwards from all the regular expressions i've ever used outside java. Why do you need to escape the "\" when it itself is an escape character for the "d"? --Derek

"Fear not the storm for this is where we grow strong."
 
A String is basically a sequence of unicode characters.
Java uses '\' to create special escape characters, so for example "\r" equates to a single character - the carriage return. To stop '\' from creating escape characters, it must escape itself, so "\\" equates to the character '\'. This sequence of characters is passed to the regular expression constructor, which then looks for escape sequences in this sequence.
For example, if you had the string "\\d+", this would be read as the characters '\', 'd' (the regexp escape sequence meaning any digit), and '+' (the metacharacter meaning 1 or more).

Confused? :)
 
The way Java regex works is as follows:
An actual expression (like one outputted and viewed in a text file) uses the escape character '\' and supports stuff like \s (whitespace) or \d (digit). For example:
Code:
SelectedDate=\d{1,2}/\d{1,2}/\d{4}
The same expression as a String literal in Java code:
Code:
"SelectedDate=\\d{1,2}/\\d{1,2}/\\d{4}"

Explanation: When using the string
Code:
c:\java
as a literal in Java, you need to use
Code:
"c:\\java"
. Likewise, you need to replace the \d with \\d. Double quotes are also a bit tricky. For example, if you wanted quotes around the date, as in the expression:
Code:
SelectedDate="\d{1,2}/\d{1,2}/\d{4}"
you need to use \" in place of ". This is a java escape character that inserts the actual double quote into the literal instead of terminating it. As a literal, this expression would be:
Code:
"SelectedDate=\"\\d{1,2}/\\d{1,2}/\\d{4}\""

I know this gets really confusing but this might help:
\ = java escape
\\ = regex escape

See for java regex details.

Hope this helps.
-HavaTheJut
 
I had to work with one language that had an embedded language inside, so in order for the embedded language to see the character sequence '\', 'd' you had to have:

[tt]
"\\\\d"

--> string interpolates to:

'\', '\', 'd'

--> top level language interpolates to:

'\', 'd'

--> sent to embedded language
[/tt]
YUCK!! :)
 
Brain fart...for some reason I was looking at it as an actual regexp. Its going in as a string; I've run into similar things with Perl using windows paths and such. Perl regexps work better! ;-) --Derek

"Fear not the storm for this is where we grow strong."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top