In your pattern "^(?=0)0", first character can be '0' or not at all
If you want the output like "&&&43.3&" then pattern you are looking for is to match any zero and replace that with '&'
You can use any regular expression inside the lookahead. (Note that this is not the case with lookbehind. I will explain why below.) Any valid regular expression can be used inside the lookahead. If it contains capturing parentheses, the backreferences will be saved. Note that the lookahead itself does not create a backreference. So it is not included in the count towards numbering the backreferences. If you want to store the match of the regex inside a backreference, you have to put capturing parentheses around the regex inside the lookahead, like this: (?=(regex)). The other way around will not work, because the lookahead will already have discarded the regex match by the time the backreference is to be saved.
Jane Dodo wrote:What should this expression evaluate to?
"000hello".replaceAll("^(?=0)0", "&");
Java 1.6 evaluates it to "&00hello", which I think is incorrect. Just need a second pair of eyes!
Jane Dodo wrote:While I am at it... given a String "00043.30", what's the easiest way to format it to "&&&43.3& "? (It's really going to be spaces, but this forum filters them away). Using any means available in Java, such as regex, DecimalFormat, etc. The most straightforward way (with character tweaking) does not look very elegant.
Jane Dodo wrote:You are right. My mistake was using a lookahead instead of a lookbehind.
Here is the code that does what I need:
That does not work for two reasons:
1 - Java does not support look behinds with infinite repetition (so no '*' and '+' inside look behinds!);
2 - If the infinite look behinds DID work, the first 0 in your string would have been replaced, but the second zero would not be replaced because it would not have a 0 at it's left (because you justed replaced it!).
Jane Dodo wrote:
That does not work for two reasons:
1 - Java does not support look behinds with infinite repetition (so no '*' and '+' inside look behinds!);
2 - If the infinite look behinds DID work, the first 0 in your string would have been replaced, but the second zero would not be replaced because it would not have a 0 at it's left (because you justed replaced it!).
Don't know, seems to work fine for me!
Piet Verdriet wrote:
Interesting, and thank you for following up: I'm going to see if I can find out if perhaps some things have changed lately.
Piet Verdriet wrote:And about point 2: I truly thought that the regex engine performed it's replacements from left to right and that these replacements influenced the characters to the right of it.
Henry Wong wrote:
Piet Verdriet wrote:
Interesting, and thank you for following up: I'm going to see if I can find out if perhaps some things have changed lately.
I don't know if it "changed lately", or was always like this... but I thought the infinite repetition restriction in look-aheads and look-behinds only applied to the split() method.... meaning... I always remember being able to use * and + in look-aheads and look-behinds, since regex was introduced in Java 1.4 (as long as they are not used in the split() method).
Piet Verdriet wrote:And about point 2: I truly thought that the regex engine performed it's replacements from left to right and that these replacements influenced the characters to the right of it.
Never thought about this... But it does make sense that it will work though. Strings are immutable. And under the covers, the replaceAll() uses the appendReplacement() and appendTail() methods, which uses a separate string buffer to create the result string.
Henry
Henry Wong wrote:
Piet Verdriet wrote:
Interesting, and thank you for following up: I'm going to see if I can find out if perhaps some things have changed lately.
I don't know if it "changed lately", or was always like this... but I thought the infinite repetition restriction in look-aheads and look-behinds only applied to the split() method.... meaning... I always remember being able to use * and + in look-aheads and look-behinds, since regex was introduced in Java 1.4 (as long as they are not used in the split() method).
Henry Wong wrote:
Piet Verdriet wrote:And about point 2: I truly thought that the regex engine performed it's replacements from left to right and that these replacements influenced the characters to the right of it.
Never thought about this... But it does make sense that it will work though. Strings are immutable. And under the covers, the replaceAll() uses the appendReplacement() and appendTail() methods, which uses a separate string buffer to create the result string.
Henry
Piet Verdriet wrote:For an explanation of the * and + being sometimes valid and sometimes invalid inside a look-behind, see: http://stackoverflow.com/questions/1536915/regex-look-behind-without-obvious-maximum-length-in-java
Henry Wong wrote:
Piet Verdriet wrote:For an explanation of the * and + being sometimes valid and sometimes invalid inside a look-behind, see: http://stackoverflow.com/questions/1536915/regex-look-behind-without-obvious-maximum-length-in-java
That's actually a very enlightling article, Piet. Thanks...
Henry
Jane Dodo wrote:
An interesting article. Any idea WHY Java does it that way? I mean, why not just count bytes as it goes and throw an exception in an unlikely event when the matched string (or whatever) is longer than Integer.MAX_VALUE characters? But then, I am a regex newbie.
Piet Verdriet wrote:
! @OP: I'd advice against your solution and have a look at my earlier suggestion (the one with the \G in it).
Jane Dodo wrote:
Piet Verdriet wrote:
! @OP: I'd advice against your solution and have a look at my earlier suggestion (the one with the \G in it).
Does not work for me, as I am only to replace leading and trailing (decimal point) zeroes, i.e. 00012003.40 should become 12003.4.
BTW, if there is a formatter that does that (i.e. in the prcess of cconverting a double to a String) that would be even cooler.
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|