Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - What does (?ms) in Regex mean?

I have following Regex in Powershell :

[regex]$regex = 
@'
(?ms).*?<DIV class=row>.*?
'@

What does (?ms) mean here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(?m) is the modifier for multi-line mode. It makes ^ and $ match the beginning and end of a line, respectively, instead of matching the beginning and end of the input.

For example, given the input:

ABC DEF
GHI

The regex ^[A-Z]{3} will match:

  1. "ABC"

Meanwhile, the regex (?m)^[A-Z]{3} will match:

  1. "ABC"
  2. "GHI"

(?s) is the modifier for single-line mode. It adds linebreaks and newlines to the list of characters that . will match.

Given the same input as before, the regex [A-Z]{3}. will match (note the inclusion of the space character):

  1. "ABC "

While the regex (?s)[A-Z]{3}. will match:

  1. "ABC "
  2. "DEF "

Despite their names, the two modes aren't necessarily mutually exclusive. In some implementations they cancel out, but, for the most part, they can be used in concert. You can use both at once by writing (?m)(?s) or, in shorter form, (?ms).

EDIT:

There are certain situations where you might want to use (?ms). The following examples are a bit contrived, but I think they serve our purpose. Given the input (note the space after "ABC"):

ABC
DEF
GHI

The regex (?ms)^[A-Z]{3}. matches:

  1. "ABC "
  2. "DEF "

While both (?m)^[A-Z]{3}. and (?s)^[A-Z]{3}. match:

  1. "ABC "

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...