Home > Software design >  RegEx: 2 double quote enclose string search in python
RegEx: 2 double quote enclose string search in python

Time:12-15

I try for the following string:

text = '"Some Text","Some Text","18.3",""I Love You, Dad"","","","Some Text"'
result = re.findall(r'""[^"] ""', text)

this result returns the following list ['""I Love You, Dad""', '"",""']

but i only want the 1st item of the list how can i remove the 2nd item from the regex. Here the "I Love you, Dad" is variable any string can be enclosed in 2 double quote. the condition here is: String enclose with 2 double quote.

CodePudding user response:

You can use

re.findall(r'(?<![^,])""([A-Za-z].*?)""(?![^,])', text)

See the regex demo. Details:

  • (?<![^,]) - a left comma boundary (start of string or a char other than a comma required immediately to the left of the current location)
  • "" - two double quotes
  • ([A-Za-z].*?) - Group 1: an ASCII letter (use [^\W\d_] to match any Unicode letter) and then any zero or more chars other than line break chars as few as possible
  • "" - two double quotes
  • (?![^,]) - a right comma boundary (end of string or a char other than a comma required immediately to the right of the current location)

CodePudding user response:

re.findall() method finds all instances of a text. re.search() method either returns None (if the pattern doesn’t match), or a re.MatchObject that contains information about the matching part of the string. This method stops after the first match

import re;
text = '"Some Text","Some Text","18.3",""I Love You, Dad"","","","Some Text"'
result = re.search(r'""[^"] ""', text)
if result != None: 
    print("% s" % (result.group(0)))
  • Related