regexpowershell

Regex to match quoted and non-quoted text, and not any part matches


Have given up on figuring this out on my own.

Sample string to work the regex on:

"this","is","a","test"

The below regex works fine for double quoted (once) and not quoted text.

[`""`]?is[`""`]?

However it matches part strings like is in this, in the sample string.

Looking to do the following, to delete (for example) "is" OR is in the sample string

$String = '"this`","is","a","test'

$ConstructedRegex = "[`""`]?is[`""`]?,?"

$String.replace($ConstructedRegex,"").trimstart(",").trimend(",")

Expected outcome: $String = "this","a","test"

Wrong match


Solution

  • First consideration is that .Replace is a string method for literal replacements, it doesn't work with regex. What you want to use is -replace, the regex operator for replacements.

    As for how you could approach the problem, since is could be quoted or not and it could be anywhere, including the beginning or end of a string, probably the easiest way to approach it is by splitting on , then filtering where the token doesn't match ^"?is"?$ (-notmatch, just like the other comparison operators, can act as a filter when there is a collection on the left-hand side) and lastly joining back with ,:

    'is,"this","is","a","test",is' -split ',' -notmatch '^"?is"?$' -join ','
    
    # Outputs: "this","a","test"
    

    If you wanted to do it via replacement, you could perhaps use this pattern however I don't see a way to do it in one go (it requires a TrimEnd):

    $result = 'is,"this","is","a","test",is' -replace '(?<=(?:^|,))"?is"?(?:,|$)'
    $result.TrimEnd(',')