Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

regex - comparison table for emacs regexp and perl compatible regular expression (PCRE)?

Is there a nice table or a cheatsheet on the web that compares the sytax of emacs regex and PCRE?

That I have to remember to escape grouping parenthesis and braces and other differences when I'm using emacs regex, it's all confusing, a syntax comparison table would be good for minimizing confusion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I will collect syntax differences that I know here. This answer is community wiki, add more if anyone wishes. Always check the preview before adding more.

When to escape ( ) { } |

In Emacs regexp, (, ), {, }, | are literal and escaped ones ((, ), {, }, |) are meta.

In Perl-compatible regexp, (, ), {, }, | are meta, and escaped ones are literal.

* and +

* is the literal star in both Emacs and Perl. If an expression starts with a star, the starting star is literal in Emacs regexp, illegal in Perl regexp.

Similarly for the plus.

Character classes

The character classes d (for digits), w (for words), s (for whitespace characters) do not work in Emacs regular expressions, but work in Perl. In Emacs, use [[:digit:]], [[:word:]], [[:space:]] instead (with double brackets). In Perl, they are also [:digit:], [:word:], [:space:] (single brackets).

w in Emacs matches ' and " too, unlike Perl. This is because text-mode syntax table has ' and " as word characters.

Backslash constructs

Of backslash constructs mentioned in Emacs Regexp Backslash, the following constructs are NOT in Perl compatible regular expressions.

` ' = < > \_< \_> sC cC

See also what < and > can do that cannot do

A, , z are NOT in Emacs. In Emacs, use instead:

` or '

Complications regarding newlines and interactive usage

See the second section in Text Pattern Matching in Emacs. It also mentions why and don't match newlines and tabs in incremental search forward for regular expression (C-M-s or M-x isearch-forward-regexp) and what to do.

Etc

Emacswiki regular expression


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...