regex - Java replaceAll() only replaces one instance -


i use string.replaceall() replace sequences of characters beginning '@', '$', or ':', , ending ' '(space). far have this:

string = string.replaceall("[@:$]+.*?(?= )", "zzzz"); 

however, regex used replaces first instance meets above criteria. so, given string:

"select title name nickname = :nickname , givenname = @givenname , familyname = $familyname" 

current (incorrect) output:

"select title name nickname = zzzz , givenname = @givenname , familyname = $familyname" 

desired output:

"select title name nickname = zzzz , givenname = zzzz , familyname = zzzz" 

how can edit regex produce desired output?

as mentioned can use following statement:

string = string.replaceall("[@:$]+[^ ]*", "zzzz"); 

[^...] matches characters except followed ^.

possible applications:

  1. one time processing of human-written files (need control outcome, there might strings containing @:$

  2. maybe modifying debug output can executed in dbms

it might safer restrict [a-za-z0-9_.]* instead of [^ ]*.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -