CRLF in RegEx

Welcome Forums General PowerShell Q&A CRLF in RegEx

Viewing 2 reply threads
  • Author
    Posts
    • #249332
      Participant
      Topics: 26
      Replies: 41
      Points: 381
      Rank: Contributor

      good morning people,

      I’m learning to use RegEx, I’m doing pretty good, but I cannot find how to deal with CRLFs in the source string.

      easy example that I tried:

      $string = ‘string on one line’
      $regex = ‘.+(on one).+’
      $string -match $regex

      True

      $string = @’
      string
      on two line
      ‘@
      $regex = ‘.+(on two).+’
      $string -match $regex

      False

      how can I build my RegEx so that it will work on a 2 lines string?

      thanks!

    • #249341
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      For . to match newline characters, you must be in single-line mode. This can be activated in a regex string by prefixing the match string with (?s).

    • #249350
      Participant
      Topics: 26
      Replies: 41
      Points: 381
      Rank: Contributor

      For . to match newline characters, you must be in single-line mode. This can be activated in a regex string by prefixing the match string with (?s).

      $string = @’ string on two line ‘@ $regex = ‘(?s).+(on two).+’ $string -match $regex

      well, that was easy.

      thanks!

Viewing 2 reply threads
  • The topic ‘CRLF in RegEx’ is closed to new replies.