PDA

View Full Version : Regular Expression Doubt - Position of hyphen inside [ ]


tagnu
07-11-2008, 03:58 PM
Hi all,

In almost all regular expressions, for example:

[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}

The position of hyphen is at the end, inside each bracket.

I understand that '-' is used to represent a range.

My doubt is that, is there any rule stating that you HAVE to put the '-' at the END of a pattern?

Thank you.

kbluhm
07-11-2008, 04:40 PM
If I understand what you're getting at, you'll want to comment out all PCRE control characters that you'd like interpreted as literals... such as the decimal, which is a wild card.

[A-Z0-9\._%\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,4}


To answer your question, when combining characters within brackets, you do not necessarily have to define the hyphen last. It could go anywhere unless you are specifying a range (A-Z, 0-9, etc).

I would imagine it is usually left at the end, when not escaped, due to the fact that it can define ranges. When placed at the end it is guaranteed to not mistakenly create a range.

Mwnciau
07-11-2008, 04:43 PM
The closing bracket (]), the caret (^) and the hyphen (-) can be included by escaping them with a backslash, or by placing them in a position where they do not take on their special meaning. [...] The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. Both [-x] and [x-] match an x or a hyphen.

From regular-expressions.info (http://www.regular-expressions.info/charclass.html)

tagnu
07-12-2008, 03:13 AM
Thank you all :)