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,我有一系列的字符串,看起来像这样: mystring1='150 33 316LOC 300-750 22" Tet', mystring2='134 ACV NOC 300-750 2-1/2" Tet' mystring1='150 33 316LOC 300-750 22" Tet' attach='"' pattern_mat

我有一系列的字符串,看起来像这样:

mystring1='150 33 316LOC 300-750 22" Tet',                       
mystring2='134 ACV NOC 300-750 2-1/2" Tet'                                              
    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
我需要写一个模式,它将在'''符号之前获取任何内容。在我的最终输出中'''也应该出现。我已经为mystring2和mystring3编写了一段代码,但是它在mystring1和mystring4中失败了

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
我正在附加我尝试的代码:

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
使用上述代码,我将获得“300-75022”作为输出

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
如果我将mystring1替换为'150 33 316L 300-750 INC“tet',我将得到'300-750 INC'作为输出

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
对于'150 33 316LOC 300-750 22“tet',我的输出应为22“
“134 ACV NOC 300-750 3-1/2”tet等的3-1/2英寸。

注释中有带
re
的解决方案。对于这种简单的情况,可以不带
re

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
lst = ['150 33 316LOC 300-750 22" Tet',
'134 ACV NOC 300-750 3-1/2" tet',
'150 33 316L 300-750 INC']

for s in lst:
    print([i for i in s.split() if i.endswith('"')])
for s in lst:
    print(re.findall(r'[^\s"]+"', s))
印刷品:

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
['22"']
['3-1/2"']
[]
使用
re

    mystring1='150 33 316LOC 300-750 22" Tet'
    attach='"'
    pattern_match = re.search('(?:\s+|$)\w*?.?\w*?.?\w*?\s?' \
    +''.join(attach.replace(' ','').split()[0]), mystring1)

    print(pattern_match.group())
lst = ['150 33 316LOC 300-750 22" Tet',
'134 ACV NOC 300-750 3-1/2" tet',
'150 33 316L 300-750 INC']

for s in lst:
    print([i for i in s.split() if i.endswith('"')])
for s in lst:
    print(re.findall(r'[^\s"]+"', s))

什么是
mystring3
?也许你只需要
r'\d[\d/-]*“
?请看,但即使
r'\S+”
也可以,请看@ScottHunter,那只是另一个字符串。@WiktorStribiżew问题是“d”只对数字有效,但即使我用say a'\w'替换它,对150 33 316L 300-750 2.2“tet”这样的情况也不起作用,在返回2“的情况下,要求是我不能在列表中获取字符串。它将通过另一个脚本生成,并将继续此脚本。但我喜欢您的方法,感谢您展示另一种方法。