[英]Extract a specific part of a string that contains bracket and percentage symbol using regex
I have the string "6324.13(86.36%)"
, and I'd like to extract out 86.36
only.我有字符串
"6324.13(86.36%)"
,我只想提取86.36
。 I used the following regex, but the output still comes with the bracket and percentage character.我使用了以下正则表达式,但 output 仍然带有括号和百分比字符。
re.search('\((.*?)\)',s).group(0)
-> (86.36%)
re.search('\((.*?)\)',s).group(0)
-> (86.36%)
I used the following instead, but it returns an array containing a string without the brackets.我改用了以下内容,但它返回一个包含不带括号的字符串的数组。 However, I still can't remove the percentage character.
但是,我仍然无法删除百分比字符。
re.findall('\((.*?)\)',s)
-> ['86.36%']
re.findall('\((.*?)\)',s)
-> ['86.36%']
I'm just not sure how to modify the regex to remove that %
as well.我只是不确定如何修改正则表达式以删除该
%
。 I know I can use string slicing to remove the last character, but I just want to resolve it within the regex expression itself.我知道我可以使用字符串切片来删除最后一个字符,但我只想在正则表达式本身中解析它。
Add a percentage sign to the regular expression, so that it's omitted from the capture group:向正则表达式添加百分号,以便从捕获组中省略它:
import re
s = "6324.13(86.36%)"
result = re.findall('\((.*?)%\)',s)
print(result) # Prints ['86.36']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.