Php 希伯来语或英语正则表达式
我正在寻找一个模式,只接受希伯来语或英语字母从2个字母到15个,可以接受1个空格。我尝试了以下代码,但它与我的字符串不匹配:Php 希伯来语或英语正则表达式,php,regex,preg-match,hebrew,Php,Regex,Preg Match,Hebrew,我正在寻找一个模式,只接受希伯来语或英语字母从2个字母到15个,可以接受1个空格。我尝试了以下代码,但它与我的字符串不匹配: <?php $subject = "שלום לך"; $regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u"; print_r(preg_match($regexp, $subject)); ?> 怎么样?那对你有用吗 [\w\u0590-\u05FF] 您的代码中有多个错误 首先,你的正则表达式 $regexp
<?php
$subject = "שלום לך";
$regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u";
print_r(preg_match($regexp, $subject));
?>
怎么样?那对你有用吗
[\w\u0590-\u05FF]
您的代码中有多个错误 首先,你的正则表达式
$regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u";
这就是它的意思:
# : regex delimiter
^ : begining of string
\p : character p
[{Hebrew}| ] : character class, one of the char : {, H, e, b, r, w, }, |, space
[a-zA-Z]{2,15}? : from 2 to 15 alphabetic char
\+ : a space followed by +
$ : end of string
# : regex delimiter
u : unicode
Unicode希伯来文字符是:\p{hebrew}
char类中不需要
|
字符串中没有
+
,末尾没有空格不需要进行不加密匹配
因此,应将其改写为:
$regexp="#^[\p{Hebrew} a-zA-Z]{2,15}$#u";
说明:
# : regex delimiter
^ : begining of string
[ : start class character
\p{Hebrew} : a hebrew character
: a space
a-zA-Z : a latin letter
] : end of class
{2,15} : previous chars 2 to 15 times
$ : end of string
# : regex delimiter
u : unicode
不返回数组,而是一个int,用于保存在字符串中找到模式的时间
然后您的脚本变成:
$subject = "שלום לך";
$regexp = "#^[\p{Hebrew} a-zA-Z]{2,15}$#u";
preg_match($regexp, $subject, $m);
print_r($m);
注-类似的正则表达式可用于正则表达式的其他方言,即C#/.Net。
var regexp = /^[\u0591-\u05F4\s]+$/gi;
return regexp.test(str)