Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Characters that Need an Escape

44 views
Skip to first unread message

Doc Trins O'Grace

unread,
Apr 14, 2017, 3:12:39 PM4/14/17
to
I was looking at the awk manual, but I didn't really see an answer to my question. Perhaps one of you more knowledgeable folks know:

What is the exhaustive list of characters that require an escape to use them as constants in a regular expression?

Thank you for your time and attention.

Have a very good good Friday!

Ed Morton

unread,
Apr 14, 2017, 6:53:49 PM4/14/17
to
Assuming you mean outside of a bracket expression in a current POSIX spec
Extended Regular Expression:

^ $ . * + ( ) [ ? | \ { }

The caveat being that `{` and `}` only NEED to be escaped if they look like an
RE interval specification, `{<digits>}`, otherwise they're already literal.

Regards,

Ed.

Geoff Clare

unread,
Apr 18, 2017, 8:41:03 AM4/18/17
to
That last part is only true for some versions of awk. POSIX says the
results are undefined, and some awks report such uses as an error:

Solaris:

$ echo foo | awk '/foo{/'
awk: /foo{/: syntax error Context is:
>>> /foo{/ <<<
$ echo foo | awk '/foo{x}/'
awk: /foo{x}/: syntax error Context is:
>>> /foo{x}/ <<<

HP-UX:

$ echo foo | awk '/foo{/'
awk: There is a regular expression error.
{} imbalance.
The source line number is 1.
The error context is
>>> /foo{/ <<<
$ echo foo | awk '/foo{x}/'
awk: There is a regular expression error.
Invalid {} repetition.
The source line number is 1.
The error context is
>>> /foo{x}/ <<<

--
Geoff Clare <net...@gclare.org.uk>

Doc Trins O'Grace

unread,
Apr 20, 2017, 7:55:02 AM4/20/17
to
Thank you, Ed and Geoff.

I guess I should have mentioned I am using Awk 3.1.1.

An old programmer using an old interpreter.

Ed Morton

unread,
Apr 20, 2017, 8:30:31 AM4/20/17
to
GNU awk 3.1.1? The only difference between that and a current awk (get a new one
- seriously!!!) are that RE intervals (e.g. `{3}`) would need to be enabled
using `--re-interval` or `--posix` in that old version so `{` and `}` are
strictly literal otherwise.

Ed.
0 new messages