简体   繁体   中英

what's wrong with this regex /^[\p{L}\p{N}. -/\\]+$/u

When using this regex /^[\\p{L}\\p{N}. -/\\\\]+$/u /^[\\p{L}\\p{N}. -/\\\\]+$/u in php I get an 'internal error while using the pattern' error. It want the matched string to be able to contain backslashes.

You have to escape the / char in the class character or use another delimiter than / .

Also escape the - or put it at the begining or at the end of the char class.

/^[\p{L}\p{N}. \-\/\\]+$/u

#^[\p{L}\p{N}. /\\-]+$#u
$str = '\\';    // = \
$str = '\\\\';  // = \\ (this is what you need for your regex)

So, knowing that, your current regex compiles to:

/^[\p{L}\p{N}. -/\]+$/u

which escapes the "]" character and breaks the regex.

Also your delimiter character "/" and "-" should be escaped inside the regex.

Corrected version:

$regex = '/^[\p{L}\p{N}. \\-\\/\\\\]+$/u';

I suppose your pattern should look like this:

/^[\p{L}\p{N}. -\/\\\]+$/u

You forgot to escape / and \\ characters in regex pattern, using .

Curly braces ( {} ) are used to set occurences of the pattern. Inside braces should be integers. Also, \\p is not valid pattern and you have to escape \\ and / with \\ in regex.

So, if you intend to match \\ followed by p , it should be \\\\p .

And, it completely confuses me, why do you have [] around regex? Is it intended to be a character group? If so, why do you have two {} inside it?

So, what's the string you want to match with this pattern?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM