Sunday, 18 September 2016

regex - Java Regular expressions issue - Can't match two strings in the same line

just experiencing some problems with Java Regular expressions.
I have a program that reads through an HTML file and replaces any string inside the @VR@ characters, i.e. @VR@Test1 2 3 4@VR@



However my issue is that, if the line contains more than two strings surrounded by @VR@, it does not match them. It would match the leftmost @VR@ with the rightmost @VR@ in the sentence and thus take whatever is in between.



For example:



@VR@Google@VR@    


My code would match



URL-GOES-HERE@VR@" target="_blank" style="color:#f4f3f1; text-decoration:none;" title="ContactUs">@VR@Google


Here is my Java code. Would appreciate if you could help me to solve this:



Pattern p = Pattern.compile("@VR@.*@VR@");
Matcher m;
Scanner scanner = new Scanner(htmlContent);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
m = p.matcher(line);

StringBuffer sb = new StringBuffer();

while (m.find()) {
String match_found = m.group().replaceAll("@VR@", "");
System.out.println("group: " + match_found);
}
}


I tried replacing m.group() with m.group(0) and m.group(1) but nothing. Also m.groupCount() always returns zero, even if there are two matches as in my example above.



Thanks, your help will be very much appreciated.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...