aspose file tools*
The moose likes Java in General and the fly likes Java Regexp help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Regexp help" Watch "Java Regexp help" New topic
Author

Java Regexp help

Martin Saegebrecht
Greenhorn

Joined: Sep 13, 2012
Posts: 2
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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?


The quieter you are, the more you are able to hear.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Martin Saegebrecht
Greenhorn

Joined: Sep 13, 2012
Posts: 2
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

Joined: May 03, 2008
Posts: 4642
    
    5


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


luck, db
There are no new questions, but there may be new answers.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

I have another solution which is very like Darryl's but without the look behind



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
 
 
subject: Java Regexp help