Forums Register Login

Java Regexp help

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

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
1
+Pie Number of slices to send: Send
I have another solution which is very like Darryl's but without the look behind



+Pie Number of slices to send: Send
 

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
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 935 times.
Similar Threads
regular expression
Swing Applications hangs with some errorr
why I got a ParseException while parsing a String with date and time?
Creating a tree nodes
Trouble compiling .class file - Tried Sun's solutions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:45:12.