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.
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.
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
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
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
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!
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:
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.
Mahesh Mak wrote:Hi,
I have a multi line status xml out of which i need to extract the status value.
<status>ok</status>
<status>Failed</status>
i cannot parse it as a xml ...
Henry Wong wrote:Hmmm.... How about this for checking odd number of backslashes?
Unfortunately, this can only check for odd number of backslashes up til 2001 backslashes, then it breaks...
To Joe Carco, don't you wish you wrote your own "boring" regex instead, now? ...![]()
Henry>
Rob Prime wrote:Which I already mentioned:
Rob Prime wrote:In the JavaDoc of java.util.regex.Pattern, do a search for lookbehind and you should find a solution. Of course this solution will again break if you do want to split at \\|, then again not at \\\| etc. That's going to be quite a bit harder.