22

is the regexp the same between languages?

for example. if i want to use it in javascript, would i have to search for regexp for javascript specifically. cause i got some cheat sheets. it just says regular expression.

i wonder if i could use this on all languages, php, javascript and so on.

4 Answers 4

21

The basics are mostly the same but there are some discrepancies between which engine powers the language, PHP and JavaScript differ since PHP uses PCRE (Perl Compatible Regular Expressions).

PHP also has the POSIX-compatible regex engine (ereg_* functions), but that is deprecated.

If you don't already use it, I suggest you try RegexBuddy. It can convert between several Regex engines.

You can find alternatives for RegexBuddy on Mac here.

1
  • but that one is just for windows i guess? im on mac
    – ajsie
    Dec 29, 2009 at 7:39
6

You might want to start out by looking here. That's my Bible when I do regexping!

Now, regex should be the same everywhere, at least the fundamentals, however there are cases where it differs from compiler to compiler (or interpreter if you will).

Those could be how you search for a specific pattern, let's take \w as an example, that's: any alphanumeric or underscore character in c# but the pattern in javascript might be different.

When you come to a special case like this, you might want to revise the above provided link.

1
  • 2
    For clarification, \w in any Regex interpreter finds a word character (alphanumeric and underscore characters), not an entire word.
    – BraedenP
    Dec 29, 2009 at 7:34
2

Regular expression synax varies slightly between languages but for the most part the details are the same. Some regex implementations support slightly different variations on how they process as well as what certain special character sequences mean.

Google is your best friend. Google for regex in the language of your choice.

0

One of the biggest variations in regex is how special characters are escaped / interpreted.

For instance, grep, vim and perl regexs differ in how to handle things like ( ) for grouping / capturing a pattern for back referencing in search & replace. IIRC, Perl uses them straight while grep and vim require them to be escaped.

Also, Perl regex may support more features than earlier regex engines. regex's that would have been simple in Perl were a major Pita in grep.

I'm not completely sure if this is a correct way to sum it up, but there are basically two major classes of regex - Posix ( grep and similar tools ) and Perl compatible ( with minor variations ).

One tool I've found useful is The Regex Coach - interactive regular expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.