简体   繁体   English

如何编写一个正则表达式来匹配由 3 个斜杠分隔的字符?

[英]how to write a regex to match characters separated by 3 slashes?

any string could be inside amongst the slashes, but there must be only 3 divisions.斜线之间可以有任何字符串,但必须只有 3 个分区。 For instance, Values that should match:例如,应匹配的值:

  1. "90/90/90/9090" “90/90/90/9090”
  2. "FDSAFDSA/90/pppppaA3/9090" “FDSAFDSA/90/pppppaA3/9090”

Values that should not match:不应该匹配的值:

  1. "90/90/90/9090/90" “90/90/90/9090/90”
  2. "FDSAFDSA/90/pppppaA3/9090/90" “FDSAFDSA/90/pppppaA3/9090/90”

I am using python and the re library, I have tried lots of combination but none of them worked:我正在使用 python 和 re 库,我尝试了很多组合,但都没有奏效:

 bool(re.match(r'^.*\/.*\/.*\/((?!\/).)*$', "90/90/90/9090/90"))
 bool(re.match(r'^.*\/.*\/.*\/((?!/).)*$', "90/90/90/9090/90"))
 bool(re.match(r'^.*\/.*\/.*\/(?!(/)$).*$', "90/90/90/9090/90"))
 bool(re.match(r'^.*\/.*\/.*\/(/).*$', "90/90/90/90/90"))
 bool(re.match(r'^.*\/.*\/.*\/.*(\/)$', "90/90/90/90/90"))

You ought to use negated character classes :您应该使用否定字符类

^[^/]*/[^/]*/[^/]*/[^/]*$

[^/] matches any character but / , so we represent a string that contains three / and anything else around them. [^/]匹配除/任何字符,因此我们表示一个包含三个/以及它们周围的任何其他字符的字符串。

In your regexes, the .在您的正则表达式中, . could match anything including / , and while you could have approximated an equivalent of negated character classes using negative lookarounds, you didn't properly apply them to every .可以匹配包括/在内的任何内容,虽然您可以使用否定查找来近似等效的否定字符类,但您没有正确地将它们应用于每个. you had : ^((?!\\/).)*\\/((?!\\/).)*\\/((?!\\/).)*\\/((?!\\/).)*$ would have worked too, although it would have been less performant.你有: ^((?!\\/).)*\\/((?!\\/).)*\\/((?!\\/).)*\\/((?!\\/).)*$也会起作用,尽管它的性能会降低。

And there's no need to escape those / , they aren't regex meta-characters.并且没有必要转义那些/ ,它们不是正则表达式元字符。 You might need to escape them in languages or tools that use / as delimiters, such as JavaScript's /pattern/ syntax or sed's s/search/replace/ substitutions.您可能需要在使用/作为分隔符的语言或工具中对它们进行转义,例如 JavaScript 的/pattern/语法或 sed 的s/search/replace/替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用正则表达式匹配 2 个字符或 5 个字符 - How to match either 2 characters or 5 characters with regex in python 如何在python中编写正则表达式来匹配这个? - How to write a regex in python to match this? 正则表达式匹配斜杠之间的条目,但不匹配斜杠 - 包括空条目 - Regex to match entries between slashes, but not slashes - including empty entries 如何使用正则表达式将字符串与特殊字符匹配 - How to match the strings with special characters using regex 如何在Python中使用正则表达式匹配重音字符? - How to match accented characters with a regex in Python? 如何检查与正则表达式不匹配的字符序列 - How to check for a sequence of characters that do not match a Regex 如何使用RegEx匹配以空格分隔的字符组? - How to match character groups separated by spaces using RegEx? 如何将变量写入 csv 文件而不用单个字符分隔? - How to write a variable to a csv file without it being separated by individual characters? Python regex 匹配包含两个或更少 o 字符的空格分隔单词 - Python regex match space-separated words that contain two or fewer o characters 如何在python中为包含逗号分隔值的文本编写正则表达式? - How to write a regex for a text including comma separated values in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM