Search code examples
regexpowershellpowershell-3.0

Regex expression to match forward slash followed by string


I need to filter a string, if it contains three strings:

/A/ABC
A_ABC
Downloaded

I have two types of lines. All contain Downloaded, but only either /A/ABC or A_ABC

Every substring has spaces to their left and right.

What are the correct regex expressions to match a line, if it contains Downloaded and ABC?

My big problem to match for ABC are either the slash or underscore on its left side.

I tried the following expression:

'\bDownloaded\b + \b/A/ABC\b | \/ABC\b'

However I don't receive the matching lines. Maybe someone has an easy fix to my try. Thank you!

/A/ and A_ are only examples. There could be any other letter or multiple letters. I just need to know if there is ABC anywhere in the line.

To be clear: I checked several other posts, which were close to my problem, but couldn't finally solve mine.


Solution

  • You could use a positive lookahead and assert the word Downloaded, then match ABC in the string

    ^(?=.*\bDownloaded\b).*ABC.*$
    

    Regex demo

    That will match:

    • ^ Assert start of the string
    • (?=.*\bDownloaded\b) Positive lookahead, assert what follows is the word Downloaded between word boundaries
    • .*ABC.* Match any character 0+ times, then ABC followed by any character 0+ times
    • $ Assert end of string