Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex compilation error [duplicate]

i am searching for a regular expression which removes an x at the beginning of a path. Accepted path separators are \, / and . The path can also start with one of these separators but it don't have to.

According to regexpal it is working but PHP throws the following error:

Compilation failed: missing terminating ] for character class at offset 24

and here is the regex:

/^([\/\.\\]?)X([\/\.\\]{1})/i

What's wrong with this regex?

like image 834
Le_Morri Avatar asked Mar 15 '26 01:03

Le_Morri


1 Answers

You are escaping ] at the end

You can instead use preg_quote() which puts a backslash in front of every character that is part of the regular expression syntax.

Also, there's no need to escape .(if it's is in character class)

like image 155
Anirudha Avatar answered Mar 17 '26 15:03

Anirudha