Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_String_Text Files - Fatal编程技术网

用于在文本文件之间查找字符串的Python正则表达式

用于在文本文件之间查找字符串的Python正则表达式,python,regex,string,text-files,Python,Regex,String,Text Files,我目前正在编写python代码,以便在'之间提取文本文件上的特定字符串。以下是我正在编写的代码: import re pattern = "(\w+'*',)" with open('C:\Documents and Settings\Ash\Desktop\strcount.txt', "r") as text: for line in text: print re.findall(pattern, line) 以及文本文件中的字符串列表 ('FLBC8U', 24)

我目前正在编写python代码,以便在
'
之间提取文本文件上的特定字符串。以下是我正在编写的代码:

import re
pattern = "(\w+'*',)"
with open('C:\Documents and Settings\Ash\Desktop\strcount.txt', "r") as text:
    for line in text:
        print re.findall(pattern, line)
以及文本文件中的字符串列表

('FLBC8U', 24)
('cHvQuW', 24)
('7FDm@', 24)
('15ii?', 24)
('H!oDe', 24)
('RB6#U', 24)
('uAmer', 24)
('6NmDJ', 24)
('d-MS1', 24)
('Ejf&B', 24)

我只想在>'/c>中的字符串>逗号>代码>单引号>,/>代码>因此忽略数字和括号

:这应该为您解决:

(?<=\(')((?!').)*(?=')
(?我的正则表达式:

(?<=\').*(?=\',)

(?尝试使用以下模式:

   \(\'(.*)\',.+\)

示例:

如果字符串仅在一行中,则可以执行以下操作:

>>> "('FLBC8U', 24)"[1:-1].split(',')[0]
"'FLBC8U'"

可能重复或请添加一些详细信息,说明您提出的解决方案回答给定问题的原因。类似于一行的答案可能会在将来被删除。@furins编辑了我的答案。
>>> "('FLBC8U', 24)"[1:-1].split(',')[0]
"'FLBC8U'"