Java regex: Normalize all escaped or non-escaped newlines - why does this fail? -
I am trying to normalize any new lines or try to avoid newlines in a new unisex newline string I am doing I can not understand why this does not work:
pattern EOL = Pattern.compile ("(\\\\ r)? \\\\ n | \ r? \ N" ); The last string saved EOL = "\\\\ n"; Println (EOL.matcher ("asdf \ njkl;") replaceAll (escapedEOL).); Println (EOL.matcher ("asdf \ n") replaceAll (escapedEOL).); Println (EOL.matcher ("asdf \ r \ njkl;") replaceAll (escapedEOL).); Println (EOL.matcher ("asdf \ r \ n") replaceAll (escapedEOL).); Println (EOL.matcher ("asdf \\ r \\ njkl,".) ReplaceAll (escapedEOL); Println (EOL.matcher ("asdf \\ r \\ n") replaceAll (escapedEOL).); Result:
asdf \ njkl; Asdf asdf \ njkl; Asdf \ n asdf \ njkl; Asdf \ ncomplete Can anyone shed any light on this? I know that I can divide it into two calls but now I'm curious ...
edit : It looks like I should work hard Quantifiers should be avoided with groups of Java 7. pattern EOL = Pattern.compile ("\\\\ n | \\\\ r \\\\ n | \ r? \ N") also works.
I'm not sure that your regex seems to work on changing, as you might Wanted, so make changes
Pattern EOL = Pattern.compile ("(\\\\ r)? \\\\ n | \ r \ n"); From to
pattern EOL = Pattern.compile ("\ r? \ N | (\\\\ r)? \\\\ n" );
Anyway, it looks like a bug more than desired behavior and was changed to Java 8 so that your original Rijks also resulted in < Pre> asdf \ njkl; Asdf \ n asdf \ njkl; Asdf \ n asdf \ njkl; Asdf \ n
Comments
Post a Comment