0

I really need some help forming a regex pattern in java.

I am trying to replace all occurrences of a given word provided that it is not enclosed within a less-than or greater-than sign. It should not replace it even if the less/greater-than signs are not directly next to the word. Here is my most recent attempt:

    stringIn = stringIn.replaceAll("((?:<[^>]>[^<]*)+" + word + "(?:<[^>]>[^<]*)+)", "$1<" + newWord + ">$2");

But this does not seem to replace the word not in brackets. Just to clarify I made up an example replacement:

    "foo word <word> <foo word foo>" should go to
    "foo <newWord> <word> <foo word foo>

PS Just saw this SO question and it looks similar but I don't really understand it and cannot guaranty it as it refers to PHP not Java.

Community
  • 1
  • 1
Saad Attieh
  • 1,396
  • 3
  • 21
  • 42

3 Answers3

1

I would use the regex

word(?=[^>]*(?:$|<))

The items contained in {} in the following explanation show what part of the regex is explained by the previous phrase: The regex looks for a word {word} that is followed by {(?=...)} a sequence of zero or more characters {[...]*} that does not include the character > {^>} and is immediately followed by the end of the string {$} or {(?:...|...)} the character < {<}.

You can find a demonstration and another explanation here: http://regex101.com/r/tY5cT7

Implemented in Java, it would look like this:

stringIn = stringIn.replaceAll(word + "(?=[^>]*(?:$|<))", "<" + newWord + ">");

Note: this assumes that all <...> are balanced, i.e. for every < there is one and only one corresponding >, and for every > there is one and only one corresponding <.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
0

Here is code that should work for you:

String newWord = "-REPLACED-";
Pattern p = Pattern.compile( "(<[^>]*>)|(\\bword\\b)" );
Matcher m = p.matcher( "foo word <word> <foo word foo> word" );
StringBuffer sb = new StringBuffer();
while (m.find()) {
    if (m.groupCount()>1 && m.group(2) != null)
        m.appendReplacement(sb, "<" + newWord + ">");
}
m.appendTail(sb);
System.out.println( sb );

OUTPUT

foo <-REPLACED-> <word> <foo word foo> <-REPLACED->
anubhava
  • 761,203
  • 64
  • 569
  • 643
-1

You can use something like this :

String val="<blablabla word blablabla> word123 <word>";
String REGEX="(?<=>)[^<]+";
String replaceString="replace";
System.out.println(val.replaceAll(REGEX, replaceString));

Output

<blablabla word blablabla>replace<word>

Refer

Sujith PS
  • 4,776
  • 3
  • 34
  • 61