简体   繁体   English

正则表达式匹配不超过一定数量的特定字符的行?

[英]Regex match lines with no more than a certain number of specific characters?

This is my regex so far (assume PHP flavour):到目前为止,这是我的正则表达式(假设 PHP 风格):

^(([^\\\\]+)\\\\([^\\\\]+)){1,4}$

And my test data:还有我的测试数据:

U:\16. New Products\#Complete\Bottle Openers\20170210 St Patrick Bottle Openers\Small Lifestyles
U:\16. New Products\#Complete\Canvas
U:\16. New Products

The goal is to find all lines with no more than 4 slashes.目标是找到不超过 4 个斜线的所有行。 In this example I expect to match the second and third lines, however when I test that in regex101 it seems to match over multiple lines, despite having multiline set and using ^ and $ .在这个例子中,我希望以匹配第二行和第三行,但是当我测试,在regex101似乎多条线路,匹配了,尽管有multiline集,并使用^$ What am I doing wrong?我究竟做错了什么?

The [^\\\\] pattern is a negated character class that matches any char but a \\ char, and thus, it can match line breaks. [^\\\\]模式是一个否定字符类,它匹配除\\字符之外的任何字符,因此,它可以匹配换行符。 To quickly fix the issue, you might add \\n (and perhaps, \\r ) to the negated character class and use要快速解决此问题,您可以将\\n (也许还有\\r )添加到否定字符类并使用

^(([^\\\n\r]+)\\([^\\\n\r]+)){1,4}$

See the regex demo .请参阅正则表达式演示 The [^\\\\\\n\\r] cannot match CR and LF symbols and matches any char but a \\ , LF and CR chars. [^\\\\\\n\\r]不能匹配 CR 和 LF 符号,并且可以匹配除\\ 、 LF 和 CR 字符之外的任何字符。

A better regex for this task would be这项任务的更好的正则表达式是

^[^\\\n\r]*(?:\\[^\\\n\r]*){0,4}$

Or, with the last quantified part set to possessive to enhance efficiency:或者,将最后一个量化的部分设置为所有格以提高效率:

^[^\\\n\r]*(?:\\[^\\\n\r]*){0,4}+$

See this regex demo .请参阅此正则表达式演示

Details细节

  • ^ - start of string ^ - 字符串的开始
  • [^\\\\\\n\\r]* - zero or more chars other than \\ , LF and CR [^\\\\\\n\\r]* - 除\\ 、 LF 和 CR 之外的零个或多个字符
  • (?:\\\\[^\\\\\\n\\r]*){0,4} - 0 to 4 occurrences of (?:\\\\[^\\\\\\n\\r]*){0,4} - 0 到 4 次
    • \\\\ - a \\ char \\\\ - 一个\\字符
    • [^\\\\\\n\\r]* - zero or more chars other than \\ , LF and CR [^\\\\\\n\\r]* - 除\\ 、 LF 和 CR 之外的零个或多个字符
  • $ - end of string. $ - 字符串的结尾。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM