Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式:给定一个字符串,在双引号中查找子字符串,在双引号中查找子字符串_Python_Regex - Fatal编程技术网

Python 正则表达式:给定一个字符串,在双引号中查找子字符串,在双引号中查找子字符串

Python 正则表达式:给定一个字符串,在双引号中查找子字符串,在双引号中查找子字符串,python,regex,Python,Regex,例如: 如果字符串为“正常”脚本,则输出应显示子字符串normal在双引号中,而子字符串script不在双引号中 为了从字符串中跟踪双引号子字符串,我尝试使用正则表达式: r'([^”]*)“' 我们可以使用split() 下面是我尝试过的代码-它返回双引号的子字符串列表 import re def demo(text): matches = re.findall(r'"([^"]*)"', text) return matches a = demo('"norma

例如:

如果字符串为“正常”脚本,则输出应显示子字符串
normal
在双引号中,而子字符串
script
不在双引号中

为了从字符串中跟踪双引号子字符串,我尝试使用正则表达式:

r'([^”]*)“'

我们可以使用
split()

下面是我尝试过的代码-它返回双引号的子字符串列表

import re
def demo(text):      
    matches = re.findall(r'"([^"]*)"', text)
    return matches

a = demo('"normal" string "is here"')
print(a)
除了寻找双引号的子字符串外,我还寻找非双引号的子字符串

import re
def demo(text):      
    matches = re.findall(r'"([^"]*)"', text)
    return matches

a = demo('"normal" string "is here"')
print(a)
例如,
demo(“'normal”string”在这里“')
的输出应该是:

双引号:
['normal','is here']

非双引号:
['string']
我知道
split()
最快,
replace()
比regex快,所以:

output = '"normal" script'.replace('"', '').split()
输出:
['normal','script']

执行时间:
3.490e-05秒
使用regex,您可以获得时间
0.2e-04
0.3e-04

我知道
split()
最快,
replace()
比regex更快,因此:

output = '"normal" script'.replace('"', '').split()
输出:
['normal','script']

执行时间:
3.490e-05秒

使用正则表达式,您可以在
0.2e-04
0.3e-04

中搜索同一正则表达式中的引号和双引号字符串

import re

def dequote(s):
    return re.findall(r'(?:"([^"]*)")|([^"]*)', s)

print(dequote('"normal" script'))
print(dequote('another "normal" script with "extra words in it"'))
请注意,返回的元组列表包含带引号的字符串和不带引号的字符串。带引号的字符串位于元组的第一个元素中,不带引号的字符串位于第二个元素中

如果你想把这些列表分开,那么把它们分开就很简单了

result = dequote('another "normal" script with "extra words in it"')

result_quoted = [t[0].strip() for t in result if t[0]]
result_unquoted = [t[1].strip() for t in result if t[1]]

print("double quoted: {}\nnot double quoted{}".format(
    result_quoted, result_unquoted))
整个程序的输出:

$ python x.py 
[('normal', ''), ('', ' script'), ('', '')]
[('', 'another '), ('normal', ''), ('', ' script with '), ('extra words in it', ''), ('', '')]
double quoted: ['normal', 'extra words in it']
not double quoted['another', 'script with']

注意,您暗示一个基于<代码> Re/Cu>的解决方案将比基于<代码> STR-SPLITE()/代码>的一个解决方案要快。我不相信这一点。考虑这两个解决方案:

def dequote_re(s):
    result = re.findall(r'(?:"([^"]*)")|([^"]*)', s)
    result_quoted = [t[0].strip() for t in result if t[0]]
    result_unquoted = [t[1].strip() for t in result if t[1]]
    return result_quoted, result_unquoted

def dequote_split(s):
    result = s.split('"')
    result_unquoted = [item.strip() for item in result[0::2] if item]
    result_quoted = [item.strip() for item in result[1::2] if item]
    return result_quoted, result_unquoted

它们给出了相同的答案。也许您应该运行timeit来查找哪个更快。

您可以在同一正则表达式中搜索引号和双引号字符串

import re

def dequote(s):
    return re.findall(r'(?:"([^"]*)")|([^"]*)', s)

print(dequote('"normal" script'))
print(dequote('another "normal" script with "extra words in it"'))
请注意,返回的元组列表包含带引号的字符串和不带引号的字符串。带引号的字符串位于元组的第一个元素中,不带引号的字符串位于第二个元素中

如果你想把这些列表分开,那么把它们分开就很简单了

result = dequote('another "normal" script with "extra words in it"')

result_quoted = [t[0].strip() for t in result if t[0]]
result_unquoted = [t[1].strip() for t in result if t[1]]

print("double quoted: {}\nnot double quoted{}".format(
    result_quoted, result_unquoted))
整个程序的输出:

$ python x.py 
[('normal', ''), ('', ' script'), ('', '')]
[('', 'another '), ('normal', ''), ('', ' script with '), ('extra words in it', ''), ('', '')]
double quoted: ['normal', 'extra words in it']
not double quoted['another', 'script with']

注意,您暗示一个基于<代码> Re/Cu>的解决方案将比基于<代码> STR-SPLITE()/代码>的一个解决方案要快。我不相信这一点。考虑这两个解决方案:

def dequote_re(s):
    result = re.findall(r'(?:"([^"]*)")|([^"]*)', s)
    result_quoted = [t[0].strip() for t in result if t[0]]
    result_unquoted = [t[1].strip() for t in result if t[1]]
    return result_quoted, result_unquoted

def dequote_split(s):
    result = s.split('"')
    result_unquoted = [item.strip() for item in result[0::2] if item]
    result_quoted = [item.strip() for item in result[1::2] if item]
    return result_quoted, result_unquoted
他们给出了相同的答案。也许您应该运行timeit来找出哪个更快。

使用模块:

有关详细说明,请参见。简单地说,将
(*SKIP)(*F)
附加到要排除的正则表达式,并使用alternation定义您需要的模块:


请参阅以获取详细解释。简单地说,将
(*SKIP)(*F)
附加到要排除的正则表达式,并使用交替定义所需的正则表达式如果字符串很大,则可以使用正则表达式计算出现的值,并设法将其拆分为较小的部分(取决于您希望获得的内容和从何处获得)

看起来你的子串是单词。 对于双引号或非双引号字符串,可以按子字符串拆分,并将其作为列表进行删除

按双引号或非双引号拆分可能需要创建两个列表

按单词拆分您可以创建一个单词列表,并在输出时检查双引号

根据您得到的字符串的大小,这两种情况的成本几乎相同

我建议您使用,并尽可能多地获取您可能需要处理的字符串片段


我的最佳选择。

如果你有一个相当大的字符串,你可以使用正则表达式来计算出现的情况,并设法将其分解成更小的部分(取决于你希望得到什么以及从哪里得到)

看起来你的子串是单词。 对于双引号或非双引号字符串,可以按子字符串拆分,并将其作为列表进行删除

按双引号或非双引号拆分可能需要创建两个列表

按单词拆分您可以创建一个单词列表,并在输出时检查双引号

根据您得到的字符串的大小,这两种情况的成本几乎相同

我建议您使用,并尽可能多地获取您可能需要处理的字符串片段


最好。

您的输入和预期输出是什么?您的代码在哪里?将使用代码更新详细信息。您的输入和预期输出是什么?您的代码在哪里?将使用代码更新详细信息。