Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expressions Problem  RSS feed

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
trying to convert then following string:-
String test="name(mark):123"; //, name(simon):, name(paul):,";
to
String test="mark:123"; //, simon:, paul:,";

trying to use Regular expressions (i.e.Pattern & Matcher), but cant get it to work.

Has anybody got any ideas?
 
Marshal
Posts: 58384
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know at the moment, but it might be helpful to tell us which regular expressions you have tried, please.
 
Mat Anthony
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,
I have been trying to use:-

StringBuffer sb = new StringBuffer();
String replacement = ??
matcher.appendReplacement(sb, replacement);
 
Mat Anthony
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,
I got the above to work with the following code, but its very messy , need help with reg exp any idea?




Results
--------
Inputting= name(mark):12, name(paul):, name(sue):56, name(wendy):3, something:,
result= mark:12, paul:, sue:56, wendy:3, something:

[edit]add code tags. CR[/edit]
 
Campbell Ritchie
Marshal
Posts: 58384
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with it? [Apart from code tags; please always use them.] It might be a neater solution than using a regular expression would have been.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mat Anthony wrote:Hi Ritchie,
...

Inputting= name(mark):12, name(paul):, name(sue):56, name(wendy):3, something:,
result= mark:12, paul:, sue:56, wendy:3, something:



Here's a regex based soluition:


But, I don't think your original solution is messy. You use meaningful variable names and it is well commented. So, if that regex above looks like meaningless drivel to you, I'd just stick with what you have.

Good luck!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!