Why does String.replaceAll() work differently in Java 8 from Java 9?What is a serialVersionUID and why should...
How to prevent cleaner from hanging my lock screen in Ubuntu 16.04
How do I say "Brexit" in Latin?
Am I a Rude Number?
Are there neural networks with very few nodes that decently solve non-trivial problems?
Would a National Army of mercenaries be a feasible idea?
We are very unlucky in my court
What is the wife of a henpecked husband called?
Can a hotel cancel a confirmed reservation?
why a subspace is closed?
Eww, those bytes are gross
Cryptic with missing capitals
Why are the books in the Game of Thrones citadel library shelved spine inwards?
What is the purpose of easy combat scenarios that don't need resource expenditure?
How to explain planetary rings pulsating?
Placing an adverb between a verb and an object?
Can a dragon be stuck looking like a human?
Why does a metal block make a shrill sound but not a wooden block upon hammering?
Every character has a name - does this lead to too many named characters?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Is a debit card dangerous for an account with low balance and no overdraft protection?
Word or phrase for showing great skill at something without formal training in it
How would one buy a used TIE Fighter or X-Wing?
If I delete my router's history can my ISP still provide it to my parents?
Slow moving projectiles from a hand-held weapon - how do they reach the target?
Why does String.replaceAll() work differently in Java 8 from Java 9?
What is a serialVersionUID and why should I use it?Regular expression to match a line that doesn't contain a word?Why does Java have transient fields?Does Java support default parameter values?How do I check if a string contains a specific 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 executing Java code in comments with certain Unicode characters allowed?Why does array[idx++]+=“a” increase idx once in Java 8 but twice in Java 9 and 10?
Why does this code output 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
add a comment |
Why does this code output 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
10
A simplification of the code that still shows the behaviour :Pattern.matches("[^[x]]", "x")
returns true with JDK8 and false with JDK9+.
– Aaron
14 hours ago
add a comment |
Why does this code output 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
Why does this code output 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
java regex string java-8 java-9
edited 13 hours ago
Boann
37.1k1290121
37.1k1290121
asked 15 hours ago
Fuyang LiuFuyang Liu
612517
612517
10
A simplification of the code that still shows the behaviour :Pattern.matches("[^[x]]", "x")
returns true with JDK8 and false with JDK9+.
– Aaron
14 hours ago
add a comment |
10
A simplification of the code that still shows the behaviour :Pattern.matches("[^[x]]", "x")
returns true with JDK8 and false with JDK9+.
– Aaron
14 hours ago
10
10
A simplification of the code that still shows the behaviour :
Pattern.matches("[^[x]]", "x")
returns true with JDK8 and false with JDK9+.– Aaron
14 hours ago
A simplification of the code that still shows the behaviour :
Pattern.matches("[^[x]]", "x")
returns true with JDK8 and false with JDK9+.– Aaron
14 hours ago
add a comment |
1 Answer
1
active
oldest
votes
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 fix was 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]]
6
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
15 hours ago
1
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
3
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
12
In case it's not obvious -- the OP can fix this inconsistency by changing[^[0-9-]]
to[^0-9-]
.
– ruakh
14 hours ago
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54946363%2fwhy-does-string-replaceall-work-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
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 fix was 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]]
6
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
15 hours ago
1
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
3
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
12
In case it's not obvious -- the OP can fix this inconsistency by changing[^[0-9-]]
to[^0-9-]
.
– ruakh
14 hours ago
add a comment |
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 fix was 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]]
6
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
15 hours ago
1
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
3
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
12
In case it's not obvious -- the OP can fix this inconsistency by changing[^[0-9-]]
to[^0-9-]
.
– ruakh
14 hours ago
add a comment |
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 fix was 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]]
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 fix was 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]]
edited 10 hours ago
answered 15 hours ago
Karol DowbeckiKarol Dowbecki
22.6k93455
22.6k93455
6
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
15 hours ago
1
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
3
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
12
In case it's not obvious -- the OP can fix this inconsistency by changing[^[0-9-]]
to[^0-9-]
.
– ruakh
14 hours ago
add a comment |
6
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
15 hours ago
1
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
3
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
12
In case it's not obvious -- the OP can fix this inconsistency by changing[^[0-9-]]
to[^0-9-]
.
– ruakh
14 hours ago
6
6
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
15 hours 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
15 hours ago
1
1
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
@WiktorStribiżew removed, thanks. Would you suggest some other online tool that supports them?
– Karol Dowbecki
15 hours ago
3
3
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
Use RegexPlanet
– Wiktor Stribiżew
15 hours ago
12
12
In case it's not obvious -- the OP can fix this inconsistency by changing
[^[0-9-]]
to [^0-9-]
.– ruakh
14 hours ago
In case it's not obvious -- the OP can fix this inconsistency by changing
[^[0-9-]]
to [^0-9-]
.– ruakh
14 hours ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54946363%2fwhy-does-string-replaceall-work-differently-in-java-8-from-java-9%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
10
A simplification of the code that still shows the behaviour :
Pattern.matches("[^[x]]", "x")
returns true with JDK8 and false with JDK9+.– Aaron
14 hours ago