Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Python 3.x - Fatal编程技术网

Python 从正则表达式结果中删除方括号和单引号

Python 从正则表达式结果中删除方括号和单引号,python,regex,python-3.x,Python,Regex,Python 3.x,我是python新手。我需要我的正则表达式返回,不带括号和单引号 现在它返回:['UP-3415'] 我希望它返回:UP-3415 下面是我的代码 import re str = "I need to extract UP-3415 from this string" result = re.findall('UP-[0-9]{4}', str) print(result) 提前感谢。re.findall返回列表中的所有匹配结果。因此,您可以将该值用于 结果[0] 或者尝试使用re.sear

我是python新手。我需要我的正则表达式返回,不带括号和单引号

现在它返回:['UP-3415']

我希望它返回:UP-3415

下面是我的代码

import re

str = "I need to extract UP-3415 from this string"
result = re.findall('UP-[0-9]{4}', str)
print(result)

提前感谢。

re.findall返回列表中的所有匹配结果。因此,您可以将该值用于
结果[0]
或者尝试使用
re.search

重新导入
str=“我需要从该字符串中提取UP-3415”
result=re.search('UP-[0-9]{4}',str)
打印(结果)#
打印(结果组(0))#UP-3415

print(结果[0]),findall返回一个数组(列表),[]谢谢。我将使用搜索和组来完成此操作,就像您的示例中所示:)