Scott
Scott

Reputation: 11186

Is the syntax for writing regular expression standardized

Is the syntax 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.

Upvotes: 3

Views: 168

Answers (3)

Amarghosh
Amarghosh

Reputation: 59451

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 has RegExp constructor that takes the escaped string as the first argument and an optional flag string as second argument.

    /^\w+$/i and new 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 as r'\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.

Upvotes: 0

Pointy
Pointy

Reputation: 413709

Simple regular expressions, mostly yes. However, across the spectrum of programming languages, there are differences.

Upvotes: 0

Oddthinking
Oddthinking

Reputation: 25282

No, there are several dialects of Regular Expressions.

They generally have many elements in common.

Some popular ones are listed and compared here.

Upvotes: 8

Related Questions