Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range out of order in character class

Tags:

regex

php

I'm getting this odd error in the preg_match() function:

Warning: preg_match(): Compilation failed: range out of order in character class at offset 54

The line which is causing this is:

preg_match("/<!--GSM\sPER\sNUMBER\s-\s$gsmNumber\s-\sSTART-->(.*)<!--GSM\sPER\sNUMBER\s-\s$gsmNumber\s-\sEND-->/s", $fileData, $matches); 

What this regular expression does is parse an HTML file, extracting only the part between:

<!--GSM PER NUMBER - 5550101 - START--> 

and:

<!--GSM PER NUMBER - 5550101 - END--> 

Do you have a hint about what could be causing this error?

like image 266
Ariod Avatar asked Aug 11 '10 06:08

Ariod


People also ask

How do you range a character in regex?

To show a range of characters, use square backets and separate the starting character from the ending character with a hyphen. For example, [0-9] matches any digit. Several ranges can be put inside square brackets. For example, [A-CX-Z] matches 'A' or 'B' or 'C' or 'X' or 'Y' or 'Z'.

What are character classes in regex?

In the context of regular expressions, a character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.

How do you negate a set of characters?

Negated Character Classes If you don't want a negated character class to match line breaks, you need to include the line break characters in the class. [^0-9\r\n] matches any character that is not a digit or a line break.

How do you escape a hyphen in regex?

In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.


2 Answers

Hi I got the same error and solved it:

  Warning: preg_match(): Compilation failed: range out of order in character class at offset <N> 

Research Phase:

.. Range out of order .. So there is a range defined which can't be used.

.. at offset N .. I had a quick look at my regex pattern. Position N was the "-". It's used to define ranges like "a-z" or "0-9" etc.

Solution

I simply escaped the "-".

 \-     

Now it is interpreted as the character "-" and not as range!

like image 119
krang Avatar answered Sep 18 '22 18:09

krang


This error is caused for an incorrect range. For example: 9-0 a-Z To correct this, you must change 9-0 to 0-9 and a-Z to a-zA-Z In your case you are not escaping the character "-", and then, preg_match try to parse the regex and fail with an incorrect range. Escape the "-" and it must solve your problem.

like image 41
Estefano Salazar Avatar answered Sep 19 '22 18:09

Estefano Salazar