Partial replacement of regular expression $ 1- $ 99

 

From js, but the same applies to java

Partial replacement of regular expressions
, such as string to replace the "I am a programmer" inside the "programmer", replaced by "engineer." Direct Pattern matching is possible, but more complicated if difficult.
Using regular can do it.

As
the original string: I am a programmer
regular: (*) (programmer) (*)
Replace: $ 1 $ 3 Engineer

As can, where $ 1 and $ 3 respectively represent the first and third brackets to matching content.

 

 

 

Can also be nested parentheses
as
the original string: I am a programmer
regular: (*) (([^ not] | [^ a] |) programmer) (*)
Replace: $ 1 $ 4 engineers

So what can replace? More complex, it will match "programmer", but can not match "not a programmer" or "programmers."
At this time there are four regular brackets, so the last match to use $ 4.

 

 

Guess you like

Origin www.cnblogs.com/kiko2014551511/p/11547393.html