locked
matching a pipe in a string RRS feed

  • Question

  • I'm doing some string comparisons and found this

    "this is a test" -match "|"

    returns True

    I don't think the pipe symbol is a regex character so why does pipe match "this is a test"?


    • Edited by Dan_Walters Wednesday, March 25, 2020 9:07 PM
    Wednesday, March 25, 2020 9:06 PM

Answers

  • 'this is a test' -match '\|'

    Stop guessing and learj PowerShell.


    \_(ツ)_/

    • Marked as answer by jrv Tuesday, March 31, 2020 11:18 PM
    Wednesday, March 25, 2020 9:13 PM

All replies

  • nevermind, just found this In regex, the pipe (|) character is a special character that means find either the part of the pattern on the left or the right side of the pipe.

    I've changed my script to "this is a test" -like "*|*"

    • Edited by Dan_Walters Wednesday, March 25, 2020 9:09 PM
    • Marked as answer by Dan_Walters Tuesday, March 31, 2020 9:47 PM
    • Unmarked as answer by jrv Tuesday, March 31, 2020 11:18 PM
    Wednesday, March 25, 2020 9:09 PM
  • 'this is a test' -match '\|'

    Stop guessing and learj PowerShell.


    \_(ツ)_/

    • Marked as answer by jrv Tuesday, March 31, 2020 11:18 PM
    Wednesday, March 25, 2020 9:13 PM
  • congratulations on knowing about escape characters in regex
    Wednesday, March 25, 2020 9:32 PM
  • It's in the help online in PowerShell along with teh pipe character.   By "stop guessing" I mean to encourage you to learn to use the help.  It will save you a tremendous amount of time and frustration.

    \_(ツ)_/

    Wednesday, March 25, 2020 10:00 PM
  • there's help online for powershell? that's amazing!
    Wednesday, March 25, 2020 10:06 PM
  • Most new users don't know that there is help built in and online.  It is the first thing that should be learned and the first thi9ng that should be checked.

    start by learning how to use help

    help help

    or

    help help -online

    or

    help help -ShowWIndow

    Good luck.  It will get easier once you learn the basics of technical programming with PowerShell.


    \_(ツ)_/

    Wednesday, March 25, 2020 10:18 PM
  • It's not specific to Powershell. The pipe character represents a "logical or" in just about every regex engine (I'm sure there's one or two that are exceptions, but I don't know for sure).

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, March 25, 2020 10:59 PM
  • Oh, for goodness sake. Do yourself a favor and see "Mastering Regular Expressions" by Jeffrey Friedle (ISBN 978-0-596-52812-6) if you plan on spending any time using regular expressions.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, March 25, 2020 11:04 PM
  • It's not specific to Powershell. The pipe character represents a "logical or" in just about every regex engine (I'm sure there's one or two that are exceptions, but I don't know for sure).

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    'tis.  It is a fundamental of the RegEx token list and must be supported in all engines.

    I am pretty sure this is also stated in "Mastering Regular Expressions" but I am too lazy to go out to my car and retrieve the book.  It may only be listed in one of the appendices.


    \_(ツ)_/

    Thursday, March 26, 2020 12:26 AM
  • [regex]::escape('|')

    \|

    Thursday, March 26, 2020 2:51 AM
  • [regex]::escape('|')

    \|

    Isn't that cheating?  Using sneaky tools.

    'this is a test' -match [regex]::escape('|')


    \_(ツ)_/

    Thursday, March 26, 2020 3:45 AM
  • I suppose that depends on your tolerance for eyestrain when coding a pattern like this:

         [regex]::Escape( '((.*?)/[^/]+).*#!' )

    Doing that by hand gives me the heebie-jeebies!

        \(\(\.\*\?\)/\[\^/]\+\)\.\*\#!

    It's a bit like using a bit and hand brace to drill a hole vs. using an electric drill motor. :-)


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, March 26, 2020 3:13 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Yang Yang

    Tuesday, March 31, 2020 9:49 AM