Php 字符串的write reg exp如何具有类似于a-b-1和a-b-1/d-e-2的结构?
对不起,我的英语不好。我有一个问题,我想比较两个不同的字符串use reg exp。第一个字符串的结构类似于a-b-1,例如:mobile-phone-1。第二个字符串的结构类似于a-b-1/d-e-2,例如:mobile-phone-1/nokia-asha-23。我怎么做?您可以使用preg_match方法或其他方法。。。此方法适用于两个不同的字符串。非常感谢 代码演示:Php 字符串的write reg exp如何具有类似于a-b-1和a-b-1/d-e-2的结构?,php,regex,codeigniter,Php,Regex,Codeigniter,对不起,我的英语不好。我有一个问题,我想比较两个不同的字符串use reg exp。第一个字符串的结构类似于a-b-1,例如:mobile-phone-1。第二个字符串的结构类似于a-b-1/d-e-2,例如:mobile-phone-1/nokia-asha-23。我怎么做?您可以使用preg_match方法或其他方法。。。此方法适用于两个不同的字符串。非常感谢 代码演示: if (preg_match("reg exp 1", string1)) { // do something } if
if (preg_match("reg exp 1", string1)) { // do something }
if (preg_match("reg exp 2", string2)) { // do something }
p/S:不应该太在意代码演示
$pattern = '#[\w]+-[\w]+-[\d]+(/[\w]+-[\w]+-[\d]+)?#';
if (preg_match($pattern, $str, $matches)){
return $matches;
}
要匹配较短的字符串,请使用:
$pattern = '#[\w]+-[\w]+-[\d]+#';
要匹配较长的:
$pattern = '#[\w]+-[\w]+-[\d]+/[\w]+-[\w]+-[\d]+#';
要将更长的字符与更多的破折号匹配,请执行以下操作:
$pattern = '#[\w]+-[\w]+-[\d]+/[\w-]+[\d]+#';
“i”最后表示区分大小写 正则表达式及其解释。逐步解决
$text1='mobile is for you--phone-341/nokia-asha-253'; //sample string 1
$text2='mobile-phone-341'; //sample string 2
$regular_expression1='(\w)'; // Word (mobile)
$regular_expression2='(-)'; // Any Single Character (-)
$regular_expression3='(\w)'; // Word (phone)
$regular_expression4='(-)'; // Any Single Character (-)
$regular_expression5='(\d+)'; // Integer Number (341)
$regular_expression6='(\/)'; // Any Single Character (/)
$regular_expression7='(\w)'; // Word (nokia)
$regular_expression8='(-)'; // Any Single Character (-)
$regular_expression9='(\w)'; // Word (asha)
$regular_expression10='(-)'; // Any Single Character (-)
$regular_expression11='(\d)'; // Integer Number (253)
if ($c=preg_match_all ("/".$regular_expression1.$regular_expression2.$regular_expression3.$regular_expression4.$regular_expression5.$regular_expression6.$regular_expression7.$regular_expression8.$regular_expression9.$regular_expression10.$regular_expression11."/is", $text1))
{
echo "a-b-1/d-e-2 format string";
}
else
{
echo "Not in a-b-1/d-e-2";
}
echo "<br>------------------------<br>"; //just for separation
if ($c=preg_match_all ("/".$regular_expression1.$regular_expression2.$regular_expression3.$regular_expression4.$regular_expression5."/is", $text2))
{
echo "a-b-1 formate string";
}
else
{
echo "Not in a-b-1 format";
}
比较两个字符串是什么意思?使用preg_match,您可以将字符串与正则表达式进行比较,并可能拆分它们。你能解释一下你需要什么吗?我会告诉你的。当你自己做这件事的时候,你也会学到更多…@Boynux我更新了我的question@EliasVan Ootegem谢谢!第一个使用/^\w+-\w+-\d+$/第二个使用/^\w+-\w+-\d+\/\w+-\w+-\d+$/请原谅!什么样的字符串可以使用这种模式?a-b-1或a-b-1/d-e-2?可用于检测两者。括号中的部分有一个问号,因为它是可选的。我想为两个不同的字符串设置两种模式,可以吗?不是随意的。我得走了,刚回来。我将更新我的答案。如果我使用长字符串$string2='a-b-1/d-c-c-c-c-c-3';对于第二种情况,它在第二个regexp中不起作用plz、escape/symbol。我只是举一个例子,但对于长字符串,我不能使用它。例如:a-b-c-d-3代表第一个字符串,e-a-b-c-d/b-d-e-r-f-4代表第二个字符串,依此类推。我运行代码并得到一个错误解析错误:语法错误,C:\xampp\htdocs\demo\preg\u match.phpy中的意外“if”T\u if您必须更改该模式。如果您有长字符串,ifpreg_match/^[a-z]+-[a-z]+-[a-z]+-[a-z]+-[a-z]+-[0-9]+$/i,$string1,请检查语法是否有错误,对不起!它不起作用。我试过你的代码,但其他情况总是发生。我的两个字符串可以是$string1='a-b-1',$string2='a-b-1/d-c-c3';或者$string1='a-b-d-f-g-a-a-z-z-c-1',$string2='a-b-1/d-c-c-c-c-c-3'等等。@KillerHale很好地阅读了这段代码。很容易理解如何编写自己的正则表达式。这很有趣。我认为如果你理解它,你就能实现你的需求。只需要很少的改动。注意:这两个代码适用于您在问题中指定的格式..哦!我对我的失败感到非常抱歉。你是个好人。我没有仔细阅读你写的东西。谢谢你的帮助!
$text1='mobile is for you--phone-341/nokia-asha-253'; //sample string 1
$text2='mobile-phone-341'; //sample string 2
$regular_expression1='(\w)'; // Word (mobile)
$regular_expression2='(-)'; // Any Single Character (-)
$regular_expression3='(\w)'; // Word (phone)
$regular_expression4='(-)'; // Any Single Character (-)
$regular_expression5='(\d+)'; // Integer Number (341)
$regular_expression6='(\/)'; // Any Single Character (/)
$regular_expression7='(\w)'; // Word (nokia)
$regular_expression8='(-)'; // Any Single Character (-)
$regular_expression9='(\w)'; // Word (asha)
$regular_expression10='(-)'; // Any Single Character (-)
$regular_expression11='(\d)'; // Integer Number (253)
if ($c=preg_match_all ("/".$regular_expression1.$regular_expression2.$regular_expression3.$regular_expression4.$regular_expression5.$regular_expression6.$regular_expression7.$regular_expression8.$regular_expression9.$regular_expression10.$regular_expression11."/is", $text1))
{
echo "a-b-1/d-e-2 format string";
}
else
{
echo "Not in a-b-1/d-e-2";
}
echo "<br>------------------------<br>"; //just for separation
if ($c=preg_match_all ("/".$regular_expression1.$regular_expression2.$regular_expression3.$regular_expression4.$regular_expression5."/is", $text2))
{
echo "a-b-1 formate string";
}
else
{
echo "Not in a-b-1 format";
}