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

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

Leave a Comment