Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Regexp help

 
Martin Saegebrecht
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am doing a java programme for extracting a specific pattern. I have worked with it for a while but cant find the solution.

Problem:
Lets say we have a String "$(22)sdfsd$(01)df$(xx)adasd$$$asda$(435345s)}{}3444444444"

What I whant is to replace all but $(dd) wit empty string "" i.e. $ followed by paranthese followed by exactly two digits and then followed by a closure paranthese should remain.

So the outcome of the actual source string in this case should be:

"$(22)$(01)"

I have tried to use negotion but it doesnt help??

Any suggestions?

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a particular reason for that approach?
Why not extract parts of string that match to the pattern you are looking for ( $(dd) ), concatenate them and you'll have the result you are looking for?
 
Henry Wong
author
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Saegebrecht wrote:
I am doing a java programme for extracting a specific pattern. I have worked with it for a while but cant find the solution.

Problem:
Lets say we have a String "$(22)sdfsd$(01)df$(xx)adasd$$$asda$(435345s)}{}3444444444"

What I whant is to replace all but $(dd) wit empty string "" i.e. $ followed by paranthese followed by exactly two digits and then followed by a closure paranthese should remain.

So the outcome of the actual source string in this case should be:

"$(22)$(01)"

I have tried to use negotion but it doesnt help??


Yea, searching for negative patterns is not really a strong suit of regex, so I don't think it can be done with replaceAll(). However, you can use find() and concat all the found strings together -- pretty standard use case actually.

Henry
 
Martin Saegebrecht
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys

I know this could be done by using loops, concats and all that stuff, I was actually trying to limit it down to one row in the code

But as you say Henry I dont know either if that could be done using replaceall. So ill guess I go for find then

/Br
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Briefly:
positive look-behind for $(\d\d) OR beginning of input
positive look-ahead for $(\d\d) OR end of input
match 0 or more of any character, reluctantly

edit: added tt tags for readability
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another solution which is very like Darryl's but without the look behind



 
Henry Wong
author
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:I have another solution which is very like Darryl's but without the look behind



I like this solution. A positive search for both what you want and don't want, and replace it with the portion that you want. Nice. I stand corrected.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic