2

I have a java regex for replacing all instances of a specific identifier in a script.

This is the search regex that searchers for the "foo" identifier:

([^\w_]|^)foo([^\w\d_]|$)

And this is the replacement:

$1bar$2

Doing a replaceAll in something like

for foo: [1,2,3];foo&&foo;

works well, it outputs

for bar: [1,2,3];bar&&bar;

However, when we apply this to a string with two instances of the identifier separated by a single character, it only replaces the first:

foo&foo

outputs

bar&foo

This happens, I think, because the first match is "bar&" and so when analyzing the rest of the string no other match is found.

Is there a way to fix this by changing the regex only?

user2009400
  • 147
  • 9
  • If all you need is replace `foo` with `bar` why can't you use : `input.replaceAll( "foo", "bar")`; Here `foo` is the regex. Is the question missing something? – SomeDude Mar 19 '17 at 21:10
  • i want to replace entire identifiers only. Lets say I have `refoo > foo`. If i were to do `input.replaceAll( "foo", "bar")` i would get `rebar > bar`. I want, instead `refoo > bar`. – user2009400 Mar 19 '17 at 21:15
  • the question is what is the difference between refoo and bar&foo (in a programmatic way I mean) –  Mar 19 '17 at 21:18
  • `refoo` is an identifier, and its not the same as `foo`. `bar&foo` are two identifiers separated by the `&` operator. In this case `bar&foo` should be replaced by `bar&bar`. By identifier I mean identifier in a Python script, for instance. – user2009400 Mar 19 '17 at 21:23

1 Answers1

1

I think you are almost looking for \bfoo\b as your regex otherwise use lookarounds (?<=\W|^)foo(?=\W|$). In both ways replacement string is bar.

Note: \d and _ are subsets of \w and [^\w] is equal to \W

revo
  • 47,783
  • 14
  • 74
  • 117
  • 1
    `(?<=\W|^)foo(?=\W|$)` is semantically same as `\bfoo\b` but is suitable for customizing character classes if needed. – revo Mar 19 '17 at 21:40