简体   繁体   English

带有条件模式的 pcre_regex,“或”和“与”条件

[英]pcre_regex with conditional patterns, "or" and "and" conditions

Am using a pcre-regex-engine to match pattern from my file/line.我正在使用pcre-regex-engine来匹配我的文件/行中的模式。 The lines am trying to find a match are:我试图找到匹配的行是:

line-001 key_one:10.20.30.40 any data goes here
line-002 key_two:11.22.33.44 any data goes here
line-003 key_off:12.32.42.52 any data goes here
line-004 key_ten:34.45.67.89 any data goes here

Now, I want to match ip patterns starting with key_one: , key_two: or key_ten: .现在,我想匹配以key_one:key_two:key_ten:开头的 ip 模式。 I need my pattern to be conditional,"match any ip pattern starting with key_one: or, key_two: or key_ten: .我需要我的模式是有条件的,“匹配以key_one:或, key_two:key_ten:开头的任何 ip 模式。

expected pattern= key_one | key_two| key_ten & (\\d+.\\d+.\\d+.\\d+)预期模式= key_one | key_two| key_ten & (\\d+.\\d+.\\d+.\\d+) key_one | key_two| key_ten & (\\d+.\\d+.\\d+.\\d+)

But, The or(|) condition works for pcre,but not the and(&) condition.但是, or(|) 条件适用于 pcre,但不适用于 and(&) 条件。 can some one help me out to use the and (&) condition?有人可以帮我使用 and (&) 条件吗? thank u.感谢你。

Your regex is almost correct: you did not consider that the spaces in the pattern are meaningful (in PCRE, outside the character classes) unless /x modifier is used, and that there is a colon between the values.您的正则表达式几乎是正确的:除非使用/x修饰符,否则您认为模式中的空格没有意义(在 PCRE 中,字符类之外),并且值之间有一个冒号。 Also, a dot must be escaped to match a literal dot.此外,必须对点进行转义以匹配文字点。

You may use您可以使用

(key_(?:one|two|ten)):(\d{1,3}(?:\.\d{1,3}){3})

that is a contracted form of这是一种契约形式

(key_one|key_two|key_ten):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})

See the regex demo查看正则表达式演示

If you need to get the IP as a match value, use \\K operator to discard all the text matched up to that operator and return only what is matched later:如果您需要获取 IP 作为匹配值,请使用\\K运算符丢弃与该运算符匹配的所有文本并仅返回稍后匹配的内容:

key_(?:one|two|ten):\K\d{1,3}(?:\.\d{1,3}){3}

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

Note that it is best practice to write the alternatives in such a way that none of them could match at the same location (to improve performance), that is why it is not a good idea to repeat the same key_ substring within the (...|...) .请注意,这是写在这样一种方式替代最佳实践,他们没有可以匹配在同一位置(以提高性能),这就是为什么它是不是一个好主意,重复同样的key_内子(...|...)

The + quantifier matches 1 or more occurrences and in IP addresses there can only be one to three digits, that is why a limiting quantifier {1,3} seems more reliable. +量词匹配 1 次或多次出现,并且在 IP 地址中只能有一到三位数,这就是限制量词{1,3}似乎更可靠的原因。

Also, to avoid unnecessary captures, just turn capturing ( (...) ) groups into non-capturing ones ( (?:...) ).此外,为了避免不必要的捕获,只需将捕获( (...) ) 组转换为非捕获组 ( (?:...) )。

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

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