A regex to detect string not enclosed in double quotes

前端 未结 3 1772
一个人的身影
一个人的身影 2020-12-16 01:25

I have a string something like this

\"quick\" \"brown\" fox jumps \"over\" \"the\" lazy dog

I need a regex to detect words not enclosed in doubl

相关标签:
3条回答
  • 2020-12-16 01:37

    Remove the first quote from the string

    0 讨论(0)
  • 2020-12-16 01:45

    use this regex:

    \s+(?<myword>([^\"\s]+)*)\s+
    

    this should be work; and get group named myword. else you need to trim your result string.

    0 讨论(0)
  • 2020-12-16 01:59

    Use lookahead/lookbehind assertions:

    (?<![\S"])([^"\s]+)(?![\S"])
    

    Example:

    >>> import re
    >>> a='"quick" "brown" fox jumps "over" "the" lazy dog'
    >>> print re.findall('(?<![\S"])([^"\s]+)(?![\S"])',a)
    ['fox', 'jumps', 'lazy', 'dog']
    

    The main thing here is lookahead/lookbehind assertions. You can say: I want this symbol before the expression but I don't want it to be a part of the match itself. Ok. For that you use assertions:

    (?<![\S"])abc
    

    That is a negative lookbehind. That means you want abc but without [\S"] before it, that means there must be no non-space character (beginning of the word) or " before.

    That is the same but in the other direction:

    abc(?![\S"])
    

    That is a negative lookahead. That means you want abc but without [\S"] after it.

    There are four differenet assertions of the type in general:

    (?=pattern)
        is a positive look-ahead assertion
    (?!pattern)
        is a negative look-ahead assertion
    (?<=pattern)
        is a positive look-behind assertion
    (?<!pattern)
        is a negative look-behind assertion 
    
    0 讨论(0)
提交回复
热议问题