Difference between regular expression modifiers (or flags) 'm' and 's'?

modifier, regex

Solution

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).

Problem

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?

Original source