m 正则表达式模式修饰符 [英] m regex pattern modifiers

查看:58
本文介绍了m 正则表达式模式修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$subject = "SIverygood \n SIverygood\n";
$pattern = '/^SI/m';

preg_match_all($pattern, $subject, $matches2,PREG_OFFSET_CAPTURE);
var_dump($matches2);

我无法理解 ms 修饰符.我想在每个换行符的开头获得 SI.我这样做是为了练习目的,以了解 m 和 s 修饰符.上面的例子只返回第一个SI.但我两个都想要.

I am unable to understand m and s modifier. I want to get SI at the beginning of every newline. I do it for practise purpose to understand the m and s modifier. The above example just return first SI. But i want both.

推荐答案

m 是一个多行修饰符,它将使起始锚点匹配每一行的开头而不是整个字符串.在第一行开头的字符串中,您有 SI 并返回它,但在第二行中,该行的开头是 space 而不是 SI.

The m is a multi-line modifier which will makes the start anchor matches the start of each line instead of whole of your string. And in your string at the start of first line you have SI and it returns it but in second line the start of the line is a space not SI.

如果您想忽略行首的空格,您可以使用以下正则表达式:

If you want to ignore the whitespaces at the leading of your line you can use following regex:

$pattern = '/^\s*(SI)/m';

并得到第一个匹配组的结果.阅读有关修饰符的更多信息 http://www.regular-expressions.info/modifiers.html

And get the result of first matched group. Read more about modifiers http://www.regular-expressions.info/modifiers.html

这篇关于m 正则表达式模式修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆