• Post Reply Bookmark Topic Watch Topic
  • New Topic

java,util.regex pains

 
Greg Jewell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm trying to parse and modify a String using the java.util.regex package. The string in question looks like this:

foo
#a b

bar
#a c

I want to remove the comment from the bar section. My code looks for "bar", and gets the end of the region where the match is made. I then do this:

Pattern pattern = Pattern.createPattern("#?(a.*));
Matcher matcher = pattern.matcher(string);
matcher.find(endIndex);

When I print matcher.group(); I get the expected "#a c". However, when I do:

matcher.replaceFirst("$1");

The comment in front of "a b" is removed. What am I doing wrong?


Thanks for any help,
Greg Jewell
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike find(), the first thing replaceFirst() does is reset the Matcher's position within the target text, so it always replaces the first sequence that matches the regex. If you want it to work on the comment following the "bar" line, the regex you use for the replace operation needs to include that info:
 
Greg Jewell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I modified my expressions to include the other information.

This would seem to make a lot of expressions much more complex than if the matcher weren't reset. This, in turn, would slow things down a lot. Is there a reason why the matcher is reset automatically?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Jewell:

This would seem to make a lot of expressions much more complex than if the matcher weren't reset. This, in turn, would slow things down a lot. Is there a reason why the matcher is reset automatically?


replaceFirst() and replaceAll() are convenience methods -- they are technically not expected to be used with find(). If you want more control, you can used find() in conjunction with appendReplacement() and appendTail(), which are the lower level methods used by the convenience methods.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!