-1

I want to match following multiline in a text file.

[1750256247]
;Rev: B,5

I tried following, however could not succeed. Could you please help me?

$fileContent | Select-String "(?smi)(^[1750256247](-|\s*$))(^;Rev: B,5$)" -AllMatches | Foreach {$.Matches} | Foreach {$.Value}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
AydinK
  • 1
  • 2

2 Answers2

0

Not exactly 100% sure what you are after, but changing your expression to this: (?smi)(^\[1750256247\](.|-|\s*$))(^;Rev: B,5$) (example here) works.

Some issues with your expression:

  1. In regex syntax, the [ and ] characters denote character sets, that is, a set of characters of which the engine will attempt to match one of. If you need to match the actual [ and ] characters, you will need to escape them through the use of the \ character.

  2. You are using the s modifier, thus allowing the period character to also match newlines, but you are not using the period character.

EDIT: As per @stribizhev's suggestion, you can use (?smi)(^\[1750256247](.|-|\s*$))(^;Rev: B,5$). Personally I would go for the first option because I consider it more readable, but opinions will most likely differ on this.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

I find multiline regex easier to write and more intuitive to read if you build the regex in a here-string, with the newlines embedded.

$regex = @'
(?ms)\[1750256247\]\s*
;Rev: B,5\s*
'@

Just paste the literal string you're trying to match into the regex and then escape and add metacharacters for things like trailing whitespace as needed.

mjolinor
  • 66,130
  • 7
  • 114
  • 135