Difference between regular expression modifiers 'm' and 's'?

一曲冷凌霜 提交于 2019-12-17 09:39:50

问题


I often forget about the regular expression modifiers m and s and their differences. What is a good way to remember them?

As I understand them, they are:

'm' is for multiline, so that ^ and $ will match beginning of string and end of string multiple times. (as divided by \n)

's' is so that the dot will match even the newline character

Often, I just use

/some_pattern/ism

But it probably is better to use them accordingly (usually "s" in my cases).

What do you think can be a good way to remember them, instead of forgetting which is which every time?


回答1:


It's not uncommon to find someone who's been using regexes for years who still doesn't understand how those two modifiers work. As you observed, the names "multiline" and "singleline" are not very helpful. They sound like they must be mutually exclusive, but they're completely independent. I suggest you ignore the names and concentrate on what they do: m changes the behavior of the anchors (^ and $), and s changes the behavior of the dot (.).

One prominent person who mixed up the modes is the author of Ruby. He created his own regex implementation based on Perl's, except he decided to have ^ and $ always be line anchors--that is, multiline mode is always on. Unfortunately, he also incorrectly named the dot-matches-everything mode multiline. So Ruby has no s modifier, but its m modifier does what s does in other flavors.

As for always using /ism, I recommend against it. It's mostly harmless, as you've discovered, but it sends a confusing message to anyone else who's trying to figure out what the regex was supposed to do (or even to yourself, in the future).




回答2:


I like the explanation in 'man perlre':

m Treat string as multiple lines.
s Treat string as single line.

With multiple lines, ^ and $ apply to individual lines (i.e. just before and after newlines).
With a single line, ^ and $ apply to the whole, and \n just becomes another character you can match.

[Wrong]By using both m and s as you described, I would expect the second one to take precedence, so you would always be in multiline mode with /ism.[/Wrong]

I didn't read far enough:
The "/s" and "/m" modifiers both override the $* setting. That is, no matter what $* contains, "/s" without "/m" will force "^" to match only at the beginning of the string and "$" to match only at the end (or just before a newline at the end) of the string. Together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.




回答3:


maybe this way, i will never forget:

when i want to match across lines (usually using .*? to match something that doesn't matter if it span across multiple line), i will naturally think of multiline, and therefore, 'm'. Well, 'm' is actually not the one, so it is 's'.

(since i already remember 'ism' so well... so i can always remember it is not 'm', then it must be 's').

other lame attempt includes:

s is for DOTALL, it is for DOT to match ALL.
m is multiline -- it is for ^ and $ to match a lot of times.



来源:https://stackoverflow.com/questions/918806/difference-between-regular-expression-modifiers-m-and-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!