I’ve seen this example:
hello=ho02123ware38384you443d34o3434ingtod38384day
echo ${hello//[0-9]/}
Which follows this syntax: ${variable//pattern/replacement}
Unfortunately the pattern field doesn’t seem to support full regex syntax (if I use . or \s, for example, it tries to match the literal characters).
How can I search/replace a string using full regex syntax?
Use sed:
Note that the subsequent
-e‘s are processed in order. Also, thegflag for the expression will match all occurrences in the input.You can also pick your favorite tool using this method, i.e. perl, awk, e.g.:
This may allow you to do more creative matches… For example, in the snip above, the numeric replacement would not be used unless there was a match on the first expression (due to lazy
andevaluation). And of course, you have the full language support of Perl to do your bidding…