Why String.replaceAll() works differently in Java 8 from Java 9What is the difference between String and...

What to do when being responsible for data protection in your lab, yet advice is ignored?

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

Why is working on the same position for more than 15 years not a red flag?

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Why would space fleets be aligned?

What is this metal M-shaped device for?

What is better: yes / no radio, or simple checkbox?

Blindfold battle as a gladiatorial spectacle - what are the tactics and communication methods?

What is the most triangles you can make from a capital "H" and 3 straight lines?

If I delete my router's history can my ISP still provide it to my parents?

Citing paywalled articles accessed via illegal web sharing

Would a National Army of mercenaries be a feasible idea?

Roman Numerals equation 1

Can a person refuse a presidential pardon?

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?

Why zero tolerance on nudity in space?

Dilemma of explaining to interviewer that he is the reason for declining second interview

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Parsing a string of key-value pairs as a dictionary

How can I install sudo without using su?

How to deal with an incendiary email that was recalled

Why did other German political parties disband so fast when Hitler was appointed chancellor?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?



Why String.replaceAll() works differently in Java 8 from Java 9


What is the difference between String and string in C#?What is reflection and why is it useful?Is Java “pass-by-reference” or “pass-by-value”?How do I read / convert an InputStream into a String in Java?Regular expression to match a line that doesn't contain a word?Why is subtracting these two times (in 1927) giving a strange result?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why is printing “B” dramatically slower than printing “#”?Why is executing Java code in comments with certain Unicode characters allowed?













8















Why this piece of code outputs 02 in java-8 but o2 in java-9 or above?



"o2".replaceAll("([oO])([^[0-9-]])", "0$2")









share|improve this question




















  • 1





    A simplification of the code that still shows the behaviour : Pattern.matches("[^[x]]", "x") returns true with JDK8 and false with JDK9+.

    – Aaron
    1 hour ago


















8















Why this piece of code outputs 02 in java-8 but o2 in java-9 or above?



"o2".replaceAll("([oO])([^[0-9-]])", "0$2")









share|improve this question




















  • 1





    A simplification of the code that still shows the behaviour : Pattern.matches("[^[x]]", "x") returns true with JDK8 and false with JDK9+.

    – Aaron
    1 hour ago
















8












8








8


5






Why this piece of code outputs 02 in java-8 but o2 in java-9 or above?



"o2".replaceAll("([oO])([^[0-9-]])", "0$2")









share|improve this question
















Why this piece of code outputs 02 in java-8 but o2 in java-9 or above?



"o2".replaceAll("([oO])([^[0-9-]])", "0$2")






java regex string java-8 java-9






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









nullpointer

43.3k10101200




43.3k10101200










asked 1 hour ago









Fuyang LiuFuyang Liu

492315




492315








  • 1





    A simplification of the code that still shows the behaviour : Pattern.matches("[^[x]]", "x") returns true with JDK8 and false with JDK9+.

    – Aaron
    1 hour ago
















  • 1





    A simplification of the code that still shows the behaviour : Pattern.matches("[^[x]]", "x") returns true with JDK8 and false with JDK9+.

    – Aaron
    1 hour ago










1




1





A simplification of the code that still shows the behaviour : Pattern.matches("[^[x]]", "x") returns true with JDK8 and false with JDK9+.

– Aaron
1 hour ago







A simplification of the code that still shows the behaviour : Pattern.matches("[^[x]]", "x") returns true with JDK8 and false with JDK9+.

– Aaron
1 hour ago














1 Answer
1






active

oldest

votes


















11














Most likely due to JDK-6609854 and JDK-8189343 which reported negative nested character classes handling (in your example [^[0-9-]]). This behavior was fixed in 9 and 10, but not backported to 8. The bug for Java 8 is explained as:




In Java, the negation does not apply to anything appearing in
nested [brackets]



So [^c] does not match "c", as you would expect.



[^[c]] does match "c". Not what I would expect.



[[^c]] does not match "c"



The same holds true for ranges or property expressions - if they're
inside brackets, a negation at an out level does not affect them.



[^a-z] is opposite from [^[a-z]]







share|improve this answer





















  • 4





    You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

    – Wiktor Stribiżew
    1 hour ago













  • @WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

    – Karol Dowbecki
    1 hour ago








  • 1





    Use RegexPlanet

    – Wiktor Stribiżew
    1 hour ago






  • 1





    In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

    – ruakh
    1 hour ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54946363%2fwhy-string-replaceall-works-differently-in-java-8-from-java-9%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














Most likely due to JDK-6609854 and JDK-8189343 which reported negative nested character classes handling (in your example [^[0-9-]]). This behavior was fixed in 9 and 10, but not backported to 8. The bug for Java 8 is explained as:




In Java, the negation does not apply to anything appearing in
nested [brackets]



So [^c] does not match "c", as you would expect.



[^[c]] does match "c". Not what I would expect.



[[^c]] does not match "c"



The same holds true for ranges or property expressions - if they're
inside brackets, a negation at an out level does not affect them.



[^a-z] is opposite from [^[a-z]]







share|improve this answer





















  • 4





    You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

    – Wiktor Stribiżew
    1 hour ago













  • @WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

    – Karol Dowbecki
    1 hour ago








  • 1





    Use RegexPlanet

    – Wiktor Stribiżew
    1 hour ago






  • 1





    In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

    – ruakh
    1 hour ago
















11














Most likely due to JDK-6609854 and JDK-8189343 which reported negative nested character classes handling (in your example [^[0-9-]]). This behavior was fixed in 9 and 10, but not backported to 8. The bug for Java 8 is explained as:




In Java, the negation does not apply to anything appearing in
nested [brackets]



So [^c] does not match "c", as you would expect.



[^[c]] does match "c". Not what I would expect.



[[^c]] does not match "c"



The same holds true for ranges or property expressions - if they're
inside brackets, a negation at an out level does not affect them.



[^a-z] is opposite from [^[a-z]]







share|improve this answer





















  • 4





    You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

    – Wiktor Stribiżew
    1 hour ago













  • @WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

    – Karol Dowbecki
    1 hour ago








  • 1





    Use RegexPlanet

    – Wiktor Stribiżew
    1 hour ago






  • 1





    In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

    – ruakh
    1 hour ago














11












11








11







Most likely due to JDK-6609854 and JDK-8189343 which reported negative nested character classes handling (in your example [^[0-9-]]). This behavior was fixed in 9 and 10, but not backported to 8. The bug for Java 8 is explained as:




In Java, the negation does not apply to anything appearing in
nested [brackets]



So [^c] does not match "c", as you would expect.



[^[c]] does match "c". Not what I would expect.



[[^c]] does not match "c"



The same holds true for ranges or property expressions - if they're
inside brackets, a negation at an out level does not affect them.



[^a-z] is opposite from [^[a-z]]







share|improve this answer















Most likely due to JDK-6609854 and JDK-8189343 which reported negative nested character classes handling (in your example [^[0-9-]]). This behavior was fixed in 9 and 10, but not backported to 8. The bug for Java 8 is explained as:




In Java, the negation does not apply to anything appearing in
nested [brackets]



So [^c] does not match "c", as you would expect.



[^[c]] does match "c". Not what I would expect.



[[^c]] does not match "c"



The same holds true for ranges or property expressions - if they're
inside brackets, a negation at an out level does not affect them.



[^a-z] is opposite from [^[a-z]]








share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









Karol DowbeckiKarol Dowbecki

22.5k93355




22.5k93355








  • 4





    You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

    – Wiktor Stribiżew
    1 hour ago













  • @WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

    – Karol Dowbecki
    1 hour ago








  • 1





    Use RegexPlanet

    – Wiktor Stribiżew
    1 hour ago






  • 1





    In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

    – ruakh
    1 hour ago














  • 4





    You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

    – Wiktor Stribiżew
    1 hour ago













  • @WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

    – Karol Dowbecki
    1 hour ago








  • 1





    Use RegexPlanet

    – Wiktor Stribiżew
    1 hour ago






  • 1





    In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

    – ruakh
    1 hour ago








4




4





You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

– Wiktor Stribiżew
1 hour ago







You can't prove the regex does not match the string at regex101, it does not support character class union. In PCRE, [^[0-9-]] matches a char that is not [, digit and - and then a ].

– Wiktor Stribiżew
1 hour ago















@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

– Karol Dowbecki
1 hour ago







@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?

– Karol Dowbecki
1 hour ago






1




1





Use RegexPlanet

– Wiktor Stribiżew
1 hour ago





Use RegexPlanet

– Wiktor Stribiżew
1 hour ago




1




1





In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

– ruakh
1 hour ago





In case it's not obvious -- the OP can fix this inconsistency by changing [^[0-9-]] to [^0-9-].

– ruakh
1 hour ago




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54946363%2fwhy-string-replaceall-works-differently-in-java-8-from-java-9%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV