[英]RegEx: 2 double quote enclose string search in python
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.这里的“我爱你,爸爸”是可变的,任何字符串都可以用 2 个双引号括起来。 the condition here is: String enclose with 2 double quote.这里的条件是:字符串用 2 个双引号括起来。
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 ([A-Za-z].*?)
- 第 1 组:一个 ASCII 字母(使用[^\W\d_]
匹配任何 Unicode 字母)然后除换行符以外的任何零个或多个字符尽可能少""
- 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) (?,[^,])
- 右逗号边界(字符串结尾或当前位置右侧需要的逗号以外的字符)re.findall() method finds all instances of a text. re.findall() 方法查找文本的所有实例。 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. re.search() 方法要么返回 None(如果模式不匹配),要么返回包含有关字符串匹配部分的信息的 re.MatchObject。 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)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.