使用regexp解析带有反斜杠的数字字符串
如何从字符串中获得3个独立的正则表达式。我只需要数字,我知道如果我使用使用regexp解析带有反斜杠的数字字符串,regex,parsing,Regex,Parsing,如何从字符串中获得3个独立的正则表达式。我只需要数字,我知道如果我使用\d它将获得所有数字'3/1/1',但我需要3个不同的regexp场景 示例: slot/daughter_slot/port 3/1/1 regexp for slot only? 3/1/10 regexp for daughter_slot only? 3/1/2 regexp for port only? 谢谢,仅适用于插槽的regexp \d+/.+/.+ (?<!
\d
它将获得所有数字'3/1/1',但我需要3个不同的regexp场景
示例:
slot/daughter_slot/port
3/1/1 regexp for slot only?
3/1/10 regexp for daughter_slot only?
3/1/2 regexp for port only?
谢谢,仅适用于插槽的regexp
\d+/.+/.+
(?<!/|\d) - Assert that the previous character is neither / nor a digit
\d+ - Match a sequence of digits
(?=/) - Assert that the next character is a /
\d+ - Match a sequence of digits
(?<=/()) - Assert that this sequence is preceded by a /...
?=\d+/ - ... which is followed by a sequence of digits followed by a /
仅适用于子插槽的regexp
\d+/.+/.+
.+/\d+/.+
(?<!/|\d) - Assert that the previous character is neither / nor a digit
\d+ - Match a sequence of digits
(?=/) - Assert that the next character is a /
\d+ - Match a sequence of digits
(?<=/()) - Assert that this sequence is preceded by a /...
?=\d+/ - ... which is followed by a sequence of digits followed by a /
仅用于端口的regexp
.+/.+/\d+
(?<=/) - Assert that the previous character is a /
\d+ - Match a sequence of digits
(?!/|\d) - Assert that the following character is neither a digit nor a /
仅适用于插槽的regexp
\d+/.+/.+
(?<!/|\d) - Assert that the previous character is neither / nor a digit
\d+ - Match a sequence of digits
(?=/) - Assert that the next character is a /
\d+ - Match a sequence of digits
(?<=/()) - Assert that this sequence is preceded by a /...
?=\d+/ - ... which is followed by a sequence of digits followed by a /
^/d+
范例
仅适用于子插槽的regexp
\/(\d+)\/
匹配组1包含数字
范例
只用于端口的regexp
\d+$
示例如果您不假设字符串以数字开头,以数字结尾,并且只包含数字或斜杠,并且只希望匹配适当的数字 注意:JavaScript不支持查找(?)? 仅限子插槽()
(?仅对子插槽进行说明
\d+/.+/.+
(?<!/|\d) - Assert that the previous character is neither / nor a digit
\d+ - Match a sequence of digits
(?=/) - Assert that the next character is a /
\d+ - Match a sequence of digits
(?<=/()) - Assert that this sequence is preceded by a /...
?=\d+/ - ... which is followed by a sequence of digits followed by a /
\d+-匹配数字序列
(?试试这个:
$re = "/(?<slot>\\d+)\\/(?<daughter_slot>\\d+)\\/(?<port>\\d+)/m";
$str = " slot/daughter_slot/port \n 3/1/1 regexp for slot only?\n 3/1/10 regexp for daughter_slot only?\n 3/1/2 regexp for port only?";
preg_match_all($re, $str, $matches);
var_dump($matches);
$re=“/(?\\d+\ \/(?\\d+\ \/(?\\d+)\/(?\\d+)/m”;
$str=“插槽/子插槽/端口\n 3/1/1 regexp仅用于插槽?\n 3/1/10 regexp仅用于子插槽?\n 3/1/2 regexp仅用于端口?”;
预匹配全部($re,$str,$MATCHS);
var_dump($matches);
然而,我觉得这很奇怪。你想用这个正则表达式实现什么?为什么?同样的道理:你使用的是什么语言和/或正则表达式风格?当我把它放在www.regexr.com
中时,它突出显示了你显示的所有3个场景的所有字符串。因此,这个字符串出现在我们的系统中,我们假设解析它并将其显示在字符串中的3个单独的列slot、Divider_slot和port中。@YesBar1听起来您应该使用捕获组,但由于我不知道您的系统是如何将正则表达式拆分为列的,nu11p01n73R的答案可能更好。否-它最多可以是3位。ex 4/32/543我可以在转换时将其用于xml^^^^^^y要测试regexp以将其添加到我的系统中,我使用www.regexr.com,它似乎总是在xml中工作,但当我将您的解决方案添加到此网站时,www.regexr.com不会突出显示它。@是的,您可以共享您尝试的内容的链接吗?www.regexr.com但它不会显示到url中。基本上,我添加了3/1/1
,3/1/10
,3/1/2
到文本,然后我放入了表达式解决方案,但没有突出显示它。由于某些原因,我无法像您那样在浏览器中显示它。好的,我看到它工作了。您可以更改标志。插槽和端口解决方案工作了。我无法使子插槽工作。插槽、子插槽、端口只是数组键名称。您可以更改这些