klumsy
klumsy

Reputation: 4261

Matching properly escaped quotes with DOTNET REGEX

I have a circumstance where I can avoid injecting a user provided string into my PowerShell Code. While I do have code the escape it correctly (duplicating each quote, and powershell with single quoted strings accepts 5 different quote characters including smart quotes, let for now lets just assume it accepts ' What i want to do is have a regex to tell me whether the string is properly escaped, and escaping is done by doubling the quotes so a string as follows

hello ' there

is bad while

hello '' there

is safe

However 3 quotes (or 5 or 7 etc) is also bad so

hello ''' there

is also dangerous

so i'm trying to find a regex that can validate that the string is properly escaped, in that there are no odd numbered single quote patterns.

I know with standard regex counting groups like this is not possible, but with dotnet capture groups i hoped to do something like this.

('\b(?<DEPTH>)|\b'(?<-DEPTH>)|[^']*)*(?(DEPTH)(?!))

but i can't get it to work.

Upvotes: 0

Views: 209

Answers (2)

Jaykul
Jaykul

Reputation: 15824

Just because it's you, @klumsy:

"(?ix:                 # ignore whitespace and comments
    ^                  # start at the beginning
    (?(D)              # if 'D' is defined...
        (?<-D>')       #    match a quote and undefine D
        |              # otherwise
        (?:
            (?<D>')    #    match a quote and define D
            |
            [^']       #    or match anything else
            )
    )+                 # as many times as we can
    (?(D)              # if 'D' is STILL defined...
        ($!)           #    then don't match
        |              # otherwise
        [^']*          #    match anything except '
    )$                 # all the way to the end
)"

This will match only those strings which always have the quotes in pairs, but not those strings where a single quote ' or an odd number of quotes ''' appear. Only works with .Net regex, as far as I'm aware.

You can, of course, omit the first and last lines, as long as you remove all whitespace and comments.

Upvotes: 6

CB.
CB.

Reputation: 60918

Why not simply replace one ' with two '':

> $a = read-host
foo ' bar
> $a
foo ' bar
> $a -replace "'","''"
foo '' bar

Upvotes: 0

Related Questions