sed: How do you match a string containing square brackets AND hyphens?
The streaming editor (sed) allows us to use the metacharacters "[" and
"]" to match any one of a number of characters. According to the
Pattern Matching chapter in O'Reilly's book Unix in a Nutshell by
Daniel Gilly, "A hyphen or close bracket (]) as the first character is
treated as a member of the list."
That's great, but what if I need to match a string that may contain
brackets AND hypens?
I need to match a C++ variable name or constant that may include any of
these characters:
A-Z
a-z
0-9
- (hypen)
_ (underscore)
> (as in ->)
[ and ]
.. (dot)
Here is what I'd like to be able to do:
s/something\([A-Za-z0-9_->\[\]\.]*\)the_rest/something_else \1
the_rest/
That does not work.
What can I do?
Why is simply escaping the [ and ] characters in the list not good
enough?
|