Question

I am looking for a java regular expression to replace any string of the pattern, (a-z)+n't (word) to not_word

For ex:

 "doesn't like" to "not_like"
 "I don't like" to "i not_like"
 "I don't like having dinner now" to "I not_like having dinner now"

I tried many things without success.

Était-ce utile?

La solution 3

You can try the regular expression:

[a-z]+n't\s*

e.g.

private static final Pattern REGEX_PATTERN = 
        Pattern.compile("[a-z]+n't\\s*", 
                        Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

public static void main(String[] args) {
    String input = "doesn't like\nI don't like\nI don't like having dinner now";

    System.out.println(
        REGEX_PATTERN.matcher(input).replaceAll("not_")
    );
}

Output:

not_like
I not_like
I not_like having dinner now

Autres conseils

You need to change (a-z) into class character [a-z], also if you want to be sure that there is other word after [a-z]+n't part you can

  • add \\S+ - non space characters - in ( ) brackets and use this group in replacement - you can do it with $x where x is group number (in your case it will be probably $1)
  • use look-ahead mechanism (?=\\S+) so this part will not be included in match, but will have to appear after [a-z]+n't.

Now to replace your data you can use for example something like this

String replacedString = yourString.replaceAll("yourRegex","yourReplacemet");
    String s = "I don't like having dinner now";
    s =s.replaceAll("(\\w+n't)\\s(\\w+)","not_$2");
    System.out.println(s);

output : I not_like having dinner now

Use Pattern and Matcher, like this:

    String text    ="doesn't like"

    String patternString1 = "([a-zA-Z]+n't )";

    Pattern pattern = Pattern.compile(patternString1);
    Matcher matcher = pattern.matcher(text);

    String replaceAll = matcher.replaceAll("not_");
    System.out.println("replaceAll   = " + replaceAll);

The outcome will be: "not_like"

the [a-zA-Z] makes sure that it also replaces the beginning of the word, or else it would have output "doesnot_like".

Also notice the space after +n't. So that the space between the words is removed and the output is not_like as you requested instead of not_ like

Remember to include java.util.regex.Matcher and java.util.regex.Pattern!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top