• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

in need of a regular expression (Java 1.5_11)

 
Jim Atharris
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • 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!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15451
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Jim Atharris wrote:You're correct! Thank you very much Piet.


Jim


You're welcome Jim.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic