• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Regexp help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another solution which is very like Darryl's but without the look behind



 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic