File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes in need of a regular expression (Java 1.5_11) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "in need of a regular expression (Java 1.5_11)" Watch "in need of a regular expression (Java 1.5_11)" New topic
Author

in need of a regular expression (Java 1.5_11)

Jim Atharris
Greenhorn

Joined: Jan 07, 2008
Posts: 28
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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Atharris
Greenhorn

Joined: Jan 07, 2008
Posts: 28
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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Jan 07, 2008
Posts: 28
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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Jan 07, 2008
Posts: 28
You're correct! Thank you very much Piet.


Jim
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Jim Atharris wrote:You're correct! Thank you very much Piet.


Jim


You're welcome Jim.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: in need of a regular expression (Java 1.5_11)
 
Similar Threads
finding the character after a specific word in a String
Looking for advice on my html escape function (or are there any pl sql apis for escaping html?)
how to replace ? with \? from string
String Tokenizer - getting the 10 th Token
escape character nightmare - please help