Regex for managing escaped characters for items like string literals

后端 未结 6 887
别跟我提以往
别跟我提以往 2020-12-17 22:21

I would like to be able to match a string literal with the option of escaped quotations. For instance, I\'d like to be able to search \"this is a \'test with escaped\\\' val

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 23:17

    If I understand what you're saying (and I'm not sure I do) you want to find the quoted string within your string ignoring escaped quotes. Is that right? If so, try this:

    /(?

    Basically:

    • Start with a single quote that isn't preceded by a backslash;
    • Match zero or more occurrences of: backslash then quote or any character other than a quote;
    • End in a quote;
    • Don't group the middle parentheses (the ?: operator); and
    • The closing quote can't be preceded by a backslash.

    Ok, I've tested this in Java (sorry that's more my schtick than Python but the principle is the same):

    private final static String TESTS[] = {
            "'testing 123'",
            "'testing 123\\'",
            "'testing 123",
            "blah 'testing 123",
            "blah 'testing 123'",
            "blah 'testing 123' foo",
            "this 'is a \\' test'",
            "another \\' test 'testing \\' 123' \\' blah"
    };
    
    public static void main(String args[]) {
        Pattern p = Pattern.compile("(? %s%n", test, m.group(1));
            } else {
                System.out.printf("%s doesn't match%n", test);
            }
        }
    }
    

    results:

    'testing 123' => testing 123
    'testing 123\' doesn't match
    'testing 123 doesn't match
    blah 'testing 123 doesn't match
    blah 'testing 123' => testing 123
    blah 'testing 123' foo => testing 123
    this 'is a \' test' => is a \' test
    another \' test 'testing \' 123' \' blah => testing \' 123
    

    which seems correct.

提交回复
热议问题