考虑以下字符串:
ab cd ef gh/
我将如何提取ab cd ef
?
我希望能够匹配到包含特定字符的第一个系列之前的第一个空格,在上面的情况下是/
.
编辑:我正在尝试设置一个配置文件来将一些日志解析到 Logstash 中,所以我想知道是否可以仅使用正则表达式规则来执行此操作。
([^\/]+)\s+[^\/]*\/
解释:
( start of Subpattern
[^\/]+ match one or more Elements that are no /.
^ is the Negation-Operator
/ needs to be escaped.
+ stands for one or more
) end of Subpattern
\s+ match one or more Whitespaces
[^\/]* match no or any amount of Characters that are not a /
* stands for Zero or any Amount
\/ match /
子模式的内容返回匹配。
例子:
preg_match('/([^\/]+)\s+[^\/]*\//', $input, $result);
print_r($result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句