Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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
Regex 完全匹配而非部分匹配的正则表达式_Regex - Fatal编程技术网

Regex 完全匹配而非部分匹配的正则表达式

Regex 完全匹配而非部分匹配的正则表达式,regex,Regex,我目前正在尝试匹配以下内容 class-11/chemistry/ class-12/physics/ class-12/english 但不是这个 class-11/chemistry/physical-chemistry/ class-12/english/a-heart-of-ocean/ 我可以写信到这里 /^class-([12]{2})/([^/]+)/?/ 但它与我不想要的部分匹配。你非常接近 您需要的是结尾处的$,它表示字符串的结尾。也可以通过执行\/来转义/ /^(cla

我目前正在尝试匹配以下内容

class-11/chemistry/
class-12/physics/
class-12/english
但不是这个

class-11/chemistry/physical-chemistry/
class-12/english/a-heart-of-ocean/
我可以写信到这里

/^class-([12]{2})/([^/]+)/?/
但它与我不想要的部分匹配。

你非常接近

您需要的是结尾处的
$
,它表示字符串的结尾。也可以通过执行
\/
来转义
/

/^(class-([12]{2})\/([^\/]+))\/?$/

Regex101演示:

在模式末尾使用$修饰符-$表示组必须位于输入的末尾。您使用的是什么语言?我使用的是php语言。更具体地说,它是wordpress。@stlawrance你检查了下面的答案了吗?Hai但它与class-11/化学/物理化学/这是不需要的:(@stlawrance在我更新的演示中,你可以逐个测试你的字符串。它很有效。