• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

in need of a regular expression (Java 1.5_11)

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I need a regular expression for the following statement:

Replace all * with a % but don't replace \* with a %

For example, this string

I*am\*not there but*I\*am here.

would become

I%am\*not there but%I\*am here.

Likewise,

I*am\*not there but*I\\*am here. <--- note the escaped \\

would become

I%am\*not there but%I\\%am here. <--- note the escaped \\

Much thanks, Jim
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Java's regex engine cannot cope with look-behinds that do not have an upper bound, you will need to limit the number of characters you will look behind. So, you really need a regex like "match a star if it does NOT have a backslash before it, or when it has an even number of backslashes in front of it". It's the "even number of back slashes" that is the problem. So, telling the regex engine to only match a star that has zero, two, four, ... or one hundred pairs of back slashes is an option:



You can de- or increase 100 to some value you see fit for your input. Note that you will need to do some extra escaping if you put the suggestion of mine in a string literal.

Good luck!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to find a regular expression yourself already? Did it not work? Please tell us what you tried yourself - JavaRanch is here to help you learn Java and find the answer yourself.
 
Jim Atharris
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

After I had sent out the help msg I came across the lookbehind:

here (http://www.regular-expressions.info/lookaround.html)

input.replaceAll("(?<!\\\\)\\*","%");

where input are the strings that I had in my original email.

But like Piet pointed out it doesn't work when the input had \\*. It did not replace that asterisk.

Thanks,Jim
>
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Atharris wrote:Yes.

After I had sent out the help msg I came across the lookbehind:

here (http://www.regular-expressions.info/lookaround.html)

input.replaceAll("(?<!\\\\)\\*","%");

where input are the strings that I had in my original email.

But like Piet pointed out it doesn't work when the input had \\*. It did not replace that asterisk.

Thanks,Jim
>



You're welcome Jim. Here's even a little explanation in case you need it:



So, in plain English:

Match a star, only when it has 0, 2, 4, 6, ... or 100 pairs of backslashes behind it, and before those pairs of backslashes, there must either be the start of the string or something other than a back slash.
 
Jim Atharris
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Piet.

I think for the value of "100", since I am in Java, couldn't I put "input.length()" (obviously without quotes). That is assuming the String that I'm doing the regular expression against is assigned to the variable called "input".

Would you agree?

Thanks,Jim
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Atharris wrote:Thanks again Piet.

I think for the value of "100", since I am in Java, couldn't I put "input.length()" (obviously without quotes). That is assuming the String that I'm doing the regular expression against is assigned to the variable called "input".

Would you agree?

Thanks,Jim



Since the {0,100} defines the repetition for two backslashes, input.length()/2 should suffice.
 
Jim Atharris
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're correct! Thank you very much Piet.


Jim
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Atharris wrote:You're correct! Thank you very much Piet.


Jim



You're welcome Jim.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic