preg_match() returns 0 although regex testers work [duplicate]

六眼飞鱼酱① 提交于 2021-01-29 05:22:12

问题


I'm trying to validate a string using /[\p{L}\s]{6,}/ and trying to match characters only (Unicode ones as well). I used regex101 to test my regex and it works for the string Владимир Алексић. However, when I use that regex in preg_match() with the same string, it always returns 0. Yet, it returns 1 if I avoid all characters except A-Za-z.

Why is that so?


回答1:


The \p and other escape sequences which work with unicode character properties don't work unless the u pattern modifier is set.

/[\p{L}\s]{6,}/u



回答2:


Use \p{Cyrillic} to match the cyrillic characters, i.e.:

/[\p{L}\p{Cyrillic}\s]{6,}/u

Source: Source



来源:https://stackoverflow.com/questions/37900273/preg-match-returns-0-although-regex-testers-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!