Is the syntax for writing regular expression standardized
Is the synt开发者_JAVA百科ax for writing regular expression standardized? That is, if I write a regular expression in C++ it will work in Python or Javascript without any modifications.
No, there are several dialects of Regular Expressions.
They generally have many elements in common.
Some popular ones are listed and compared here.
Simple regular expressions, mostly yes. However, across the spectrum of programming languages, there are differences.
No, here are some differences that comes to mind:
JavaScript lets you write inline regex (where
\
in\s
need not be escaped as\\s
), that are delimited by the/
character. You can specify flags after the closing/
. JS also hasRegExp
constructor that takes the escaped string as the first argument and an optional flag string as second argument./^\w+$/i
andnew RegExp("^\\w+$", "i")
are valid and the same.In PHP, you can enclose the regex string inside an arbitrary delimiter of your choice (not sure of the super set of characters that can be used as delimiters though). Again you should escape backslashes here.
"|[0-9]+|"
is same as#[0-9]+#
Python and C# supports raw strings (not limited to regex, but really helpful for writing regex) that lets you write unescaped backslashes in your regex.
"\\d+\\s+\\w+"
can be written asr'\d+\s+\w+'
in Python and@'\d+\s+\w+'
in C#Delimiters like
\<
,\A
etc are not globally supported.JavaScript doesn't support lookbehind and the
DOTALL
flag.
精彩评论