An example of StringReplace
and RegularExpression
:
StringReplace["czAB26fgsgAB5ac77bd", RegularExpression["AB[0-9]"] -> "XXX"]
"czXXX6fgsgXXXac77bd"
The above example is easy.
Now to get "czAB2XXX6fgsgAB5XXXac77bd", I tried
StringReplace["czAB26fgsgAB5ac77bd", v : RegularExpression["AB[0-9]"] -> (v <> "XXX")]
"czAB2XXX6fgsgAB5XXXac77bd"
I succeeded to get the result, but with an error message :
StringJoin::string: String expected at position 1 in v<>XXX.
What is the reason for the error message? What is the correct method? (not showing the error message)
->
by:>
? $\endgroup$a<>b
, then MMA will say a,b must be string. The question can be understood in the similar context. $\endgroup$->
it will evaluatev <> ...
immediately, whenv
is just a symbol and not yet a string, and give the warning. With:>
it will evaluatev <> ...
only later, whenv
is an actual string. $\endgroup$StringExpression
syntax:StringReplace["czAB26fgsgAB5ac77bd", k : ("AB" ~~ DigitCharacter) :> ToString@k <> "XXX"]
$\endgroup$