How to use angular brackets into patterns and groups of a regular expression?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Giovanni_Mondo
Posted On:   Monday, December 15, 2008 08:15 AM

I would like to remove references from HTML. I used Pattern and Matcher classes and I got an unexpected result. I try to resume it with the following code: String regex = "(?i) (.*) "; String target = " "; String text = "head "+target+" tail"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); assertEquals(true, matcher.find()); assertEquals(1, matcher.groupCount()); assertEquals(target, matcher.group(1)); The target is found only if it has no closing angular bracket or if the regex doesn't contain angular brackets (as in "(?i)aaa(.*)bbb" ).    More>>

I would like to remove references from HTML. I used Pattern and Matcher classes and I got an unexpected result. I try to resume it with the following code:

			
String regex = "(?i) (.*) ";
String target = " ";
String text = "head "+target+" tail";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);

assertEquals(true, matcher.find());
assertEquals(1, matcher.groupCount());
assertEquals(target, matcher.group(1));


The target is found only if it has no closing angular bracket or if the regex doesn't contain angular brackets (as in "(?i)aaa(.*)bbb" ).



Could someone help me to understand why?



Many thanks,



Giovanni    <<Less
About | Sitemap | Contact