Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 确保字符串处于特定模式_Php_Regex - Fatal编程技术网

Php 确保字符串处于特定模式

Php 确保字符串处于特定模式,php,regex,Php,Regex,什么是正则表达式以确保字符串处于以下模式 字符串::如果有多个字符串,则使用|作为分隔符 例如: 1::字符串(ok) 1::詹姆斯是个男孩| 2::你好(好) 1:詹姆斯是个男孩| 2:你好(不好-单列) 1:String,2:Hellos(不正常-分隔符是逗号) 我尝试了以下代码: $pattern = '/\w::\w|/'; $string = "1::Strings|1::Strings"; preg_match($pattern , $string, $match); 你可以试试这

什么是正则表达式以确保字符串处于以下模式

字符串::如果有多个字符串,则使用|作为分隔符

例如:

1::字符串(ok)

1::詹姆斯是个男孩| 2::你好(好)

1:詹姆斯是个男孩| 2:你好(不好-单列)

1:String,2:Hellos(不正常-分隔符是逗号)

我尝试了以下代码:

$pattern = '/\w::\w|/'; $string = "1::Strings|1::Strings"; preg_match($pattern , $string, $match);

你可以试试这样的正则表达式

^(?:(\w+::\w[^:]+)\|)*(?1)$
  • (\w+:\w[^:]+)
    子模式是第一个模式中定义的格式
  • ^(?:
    ..
    \\\\\\\)*
    任何数量的子模式,其中带有尾随
    \
  • (?1)
    至少需要一次

$pattern='/\w::\w |/'$string=“1::Strings | 1::Strings”;预匹配($pattern,$string,$match);如果有多个挑战项,如何强制使用分隔符修改了一点^(?(\w+::\w[^:]+)\\\124;\ s?*(?1)$-以在分隔符之后或之前留出空间