1

This is my first post. I have problem with String in Java. I want to use regular expression. Thank you in advance for any help.

My String is:

String str = "abXYxyzXYZabXasdfggXYZ".

I want to replace all characters with "+" but not variable below:

String word = "XYZ"

So the output should be like that:

+++++++XYZ+++++++++XYZ.

I started out with:

str.replaceAll("[^" + word + "]", "+")

But I got different output like that:

++XY+++XYZ++X++++++XYZ
4
  • java.util.regex does not support a construct that matches any text other than a certain multicharacter string of text. Almost all regex flavors do not support it. Commented Nov 28, 2017 at 14:15
  • Possible duplicate of codingBat plusOut using regex Commented Nov 28, 2017 at 14:37
  • That is because [...] in regex denotes a set of single characters. You can't that easily denote the inverse of whole words. So [^XYZ] matches every character that is not X, not Y and not Z, so the whole alphabet except X, Y, Z. It does not match whole words that are unequal to the word XYZ. Commented Nov 28, 2017 at 14:55
  • Possible duplicate of Negating a set of words via java regex Commented Nov 28, 2017 at 14:55

1 Answer 1

5

Brief

This can actually be accomplished. It's an interesting approach since Java doesn't allow regex If clauses, but you can actually use a combination of lookaheads and lookbehinds within a negative lookahead to mimic a sort of If clause.


Code

See regex in use here

(?!(?:(?=XYZ)|(?<=X)(?=YZ)|(?<=XY)(?=Z))).

Replacement: +

Usage

Below code auto-generated from regex101.
See code below in use here

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "(?!(?:(?=XYZ)|(?<=X)(?=YZ)|(?<=XY)(?=Z))).";
final String string = "abXYxyzXYZabXasdfggXYZ";
final String subst = "+";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);

Results

Input

abXYxyzXYZabXasdfggXYZ

Output

+++++++XYZ+++++++++XYZ


Explanation

  • (?!(?:(?=XYZ)|(?<=X)(?=YZ)|(?<=XY)(?=Z))) Negative lookahead ensuring what follows doesn't match
  • (?:(?=XYZ)|(?<=X)(?=YZ)|(?<=XY)(?=Z)) Match either of the following
    • (?=XYZ) Positive lookahead ensuring what follows matches XYZ literally
    • (?<=X)(?=YZ) Match the following
      • (?<=X) Positive lookbehind ensuring what precedes is X literally
      • (?=YZ) Positive lookahead ensuring what follows is YZ literally
    • (?<=XY)(?=Z) Match the following
      • (?<=XY) Positive lookbehind ensuring what precedes is XY literally
      • (?=Z) Positive lookahead ensuring what follows is Z literally
  • . Match any character
Sign up to request clarification or add additional context in comments.

2 Comments

The regex101 link you posted has another regex in it than your post
@MichaelA.Schaffrath thanks, I updated the regex to version 2 but forgot to update the link here. Fixed now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.