Using regular expressions
I am having trouble trying to use a regular expression that I used in JavaScript. The web page may contain:
<b>Renewal Date:</b> 03 May 2010</td>
I just want to pull out May 03, 2010 remembering that the web page has more than just the aforementioned content. The way I'm currently doing it with JavaScript:
DateStr = /<b>Renewal Date:<\/b>(.+?)<\/td>/.exec(returnedHTMLPage);
I've tried to follow some tutorials on java.util.regex.Pattern
and off java.util.regex.Matcher
with no luck. I can't seem to translate (. +?) Into something they can understand.
thanks,
Noeneel
a source to share
This is how regular expressions are used in java:
Pattern p = Pattern.compile("<b>Renewal Date:</b>(.+?)</td>");
Matcher m = p.matcher(returnedHTMLPage);
if (m.find()) // find the next match (and "generate the groups")
System.out.println(m.group(1)); // prints whatever the .+? expression matched.
There are other useful methods in the Matcher class such as m.matches()
. Have a look at http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html
a source to share
On matches
vsfind
The problem is what you were using matches
when you should have used find
. From the API :
- The method
matches
tries to match the entire input sequence with a pattern.- The method
find
scans the input sequence looking for the next sub-series that matches the pattern.
Note that it String.matches(String regex)
also searches for a complete match of the entire string. Unfortunately, it String
doesn't give a partial match to the regular expression, but you can always s.matches(".*pattern.*")
.
About the reluctant quantifier
Java understands perfectly (.+?)
.
Here's a demo: you are presented with a string s
, which consists of a string t
repeated at least twice. Find t
.
System.out.println("hahahaha".replaceAll("^(.+)\\1+$", "($1)"));
// prints "(haha)" -- greedy takes longest possible
System.out.println("hahahaha".replaceAll("^(.+?)\\1+$", "($1)"));
// prints "(ha)" -- reluctant takes shortest possible
About escaping metacharacters
It should also be said that you have embedded \
in your regex ( "\\"
as a Java string literal) unnecessarily.
String regexDate = "<b>Expiry Date:<\\/b>(.+?)<\\/td>";
^^ ^^
Pattern p2 = Pattern.compile("<b>Expiry Date:<\\/b>");
^^
\
is used to remove regular expression metacharacters. A is /
NOT a regular expression metacharacter.
see also
a source to share
Ok, so using aioobe's original suggestion (which I also tried earlier) I have:
String regexDate = "<b>Expiry Date:</b>(.+?)</td>";
Pattern p = Pattern.compile(regexDate);
Matcher m = p.matcher(returnedHTML);
if (m.matches()) // check if it matches (and "generate the groups")
{
System.out.println("*******REGEX RESULT*******");
System.out.println(m.group(1)); // prints whatever the .+? expression matched.
System.out.println("*******REGEX RESULT*******");
}
The IF statement must continue to appear FALSE because ******* REGEX RESULT ******* is never output.
If anyone missed what I am trying to achieve, I just want to get the date. Among the html page is a date like this <b>Expiry Date:</b> 03 May 2010</td>
and I want May 03, 2010.
a source to share