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:
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?
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.
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