Differences between two regular expressions


Do anybody know why this regex:

/^(([a-zA-Z0-9\(\)áéíóúÁÉÍÓÚñÑ,\.°-]+ *)+)$/

works but this one doesn't:

/^(([a-zA-Z0-9áéíóúÁÉÍÓÚñÑ,\.°-\(\)]+ *)+)$/

The difference is the place where the parenthesis are... I tryed with some online PHP regex testers and got the same result. The second one simply doesn't work...

PHP returns:

preg_match(): Compilation failed: range out of order in character class at offset 44 in...

This is not a critic question because I've managed to make it work but I have the curiosity!

Maybe the unicode characters are changing something?


When the - character is used inside of brackets (indicating a character set) it indicates a range unless it is the last character in the set, first character in the set, or directly after the opening negating character. Then it means a literal dash. By moving it from the end to the middle you changed its meaning. If you want to keep it in the middle you will need to escape it: \-.


If the hyphen is placed as the first or last character in the character class, it is treated as a literal - (as opposed to a range), and as a result do not require escaping.

These are the positions where the hyphen do not need to be escaped:

<ul><li>right after the opening bracket ([), or</li> <li>right before the closing bracket (]), or </li> <li>right after the negating caret (^)</li> </ul>

In the second regular expression, you're placing the hyphen in the middle, and the regular expression engine tries to create a range with the character before the hyphen, the character after the hyphen, and all characters that lie between them in numerical order. As such a range isn't possible, an error message is triggered. See <a href="http://www.asciitable.com/" rel="nofollow">asciitable.com</a> for the character table.

Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.


At your first regex you've managed every thing correctly even that - hyphen which is at the end of it. well it should be there too! I mean it has two places if you don't want to escape it, one place is at the end of char class and the other one at the beginning of char class!

You guessed nice! otherwise you should escape it!


  • SKSpriteNode does not have member named ''- how to access a variable declared in my spawn
  • Perl critic error on splitting string into array using regexp
  • Query condition cannot be negated?
  • How to refactor “if” statement into an “unless” statement?
  • C++ iomanip Alignment
  • Hiding strings in Obfuscated code
  • NetworkCredential.Domain Property
  • Is there any way to distinguish between an unset property and a property set to undefined? [duplicat
  • Extend a regular expression to negative number
  • Correspondece between Git client and server versions
  • Why is negation of a regex needed?
  • How do I include superscripts in NSString?
  • Delphi. Analog of Memo/RichEdit
  • True privateness in Python
  • How to remove last utf8 char of a python string
  • How to define an array of floats in Shader properties?
  • C function strchr - How to calculate the position of the character?
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • A new chart every sheet
  • Trying to get the char code of ENTER key
  • x64 applications using gdi+: what are the consequences on performance?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • javaw.exe and eclipse startup problems
  • swift auto completion not working in Xcode6-Beta
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Knitr HTML Loop - Some HTML output, some R output
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • Arrays break string types in Julia
  • Cannot Parse HTML Data Using Android / JSOUP
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Java static initializers and reflection
  • need help with bizarre java.net.HttpURLConnection behavior
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app