• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression and Pattern match in java  RSS feed

 
Ronan Dowd
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm using Java 1.6 and want to write code which searches for a particular piece of text between 2 delimeters and then changes that text.

I've a string as below and I want to programmatically replace any text between the "[START]" and "[END]" delimeters with the text "Running"

"[START]Not Running[END]"

The above would then get updated to be:

"[START]Running[END]"

Any help would be great. Perhaps I use the matches method in Pattern ? But not sure how I change text in between 2 matches.

Thanks - Ro

 
Ronan Dowd
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I just experimented and ..




//this does print "Not Running" .. but how do I change this to "Running"

Thanks - Ro
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've set up one group by using the parentheses. Just set up groups for the other matching sections as well. (Also, how'd you get it working without escaping the brackets?) I'd do something like this:




It might be more efficient to use the String.indexOf() and String.substring() methods though.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the API for String, you might find a method you can use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!