简体   繁体   English

使用正则表达式匹配多个术语

[英]Matching multiple terms with regex

This is response to my previous thread ( Matching data from file with regex )这是对我之前的帖子的回应(将文件中的数据与正则表达式匹配

However it was matching only one keyword "DATA", but the file contains 20 others in the same format, but I only want to pull out 3 specific ones including "DATA", which "STAT", "REG", "NET"但是它只匹配一个关键字“DATA”,但该文件包含其他 20 个相同格式的关键字,但我只想提取 3 个特定的关键字,包括“DATA”,其中“STAT”、“REG”、“NET”

Is there anyway match these specific terms?反正有匹配这些特定条款吗?

Thank you in advance!先感谢您!

Assuming this is the one you're using now:假设这是您现在正在使用的:

preg_match_all('/^\s*.?"(?:DATA|STAT|REG|NET)" (\d+)\.(\d+)\s*$/m', $str, $matches);

Simply by grouping the different alternatives in a subpattern (seperated by pipes) you can easily match different values.只需将不同的备选方案分组到一个子模式中(由管道分隔),您就可以轻松匹配不同的值。 Note that the?: symbol in the beginning of the parantheses ignores capture for that subpattern.请注意,括号开头的?: 符号忽略了该子模式的捕获。

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

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