I wanted to match contents inside the parentheses (one with "per contract", but omit unwatned elements like "=" in the 3rd line) like this:
我想匹配括号内的内容(一个用“每个合约”,但在第三行中省略未编辑的元素,如“=”),如下所示:
1/100 of a cent ($0.0001) per pound ($6.00 per contract) and
.001 Index point (10 Cents per contract) and
$.00025 per pound (=$10 per contract)
I'm using the following regex:
我正在使用以下正则表达式:
r'.*?\([^$]*([\$|\d][^)]* per contract)\)'
This works well for any expression inside the parentheses which starts of with a $
, but for the second line, it omits the 1
from 10 Cents
. Not sure what's going on here.
这适用于以$开头的括号内的任何表达式,但对于第二行,它省略了10分中的1。不知道这里发生了什么。
4 个解决方案
#1
1
for the second line, it omits the 1 from 10 Cents. Not sure what's going on here.
对于第二行,它省略了10分中的1。不知道这里发生了什么。
What's going on is that [^$]*
is greedy: It'll happily match digits, and leave just one digit to satisfy the [\$|\d]
that follows it. (So, if you wrote (199 cents
you'd only get 9
). Fix it by writing [^$]*?
instead:
发生的事情是[^ $] *是贪婪的:它会愉快地匹配数字,只留下一个数字来满足它后面的[\ $ | \ d]。 (所以,如果你写了(199美分,你只得到9)。修改它来写[^ $] *?代替:
r'.*?\([^$]*?([\$|\d][^)]* per contract)\)'
#2
2
You could probably use a less specific regex
您可以使用不太具体的正则表达式
re.findall(r'\(([^)]+) per contract\)', str)
This will match the "$6.00" and the "10 Cents."
这将匹配“$ 6.00”和“10美分”。
#3
0
You can use:
您可以使用:
r'(?<=\()[^=][^)]*? per contract(?=\))'
#4
0
This will match the output you specified in your comments:
这将匹配您在评论中指定的输出:
re.search('\((([^)]+) per contract)\)', str).group(1)
#1
1
for the second line, it omits the 1 from 10 Cents. Not sure what's going on here.
对于第二行,它省略了10分中的1。不知道这里发生了什么。
What's going on is that [^$]*
is greedy: It'll happily match digits, and leave just one digit to satisfy the [\$|\d]
that follows it. (So, if you wrote (199 cents
you'd only get 9
). Fix it by writing [^$]*?
instead:
发生的事情是[^ $] *是贪婪的:它会愉快地匹配数字,只留下一个数字来满足它后面的[\ $ | \ d]。 (所以,如果你写了(199美分,你只得到9)。修改它来写[^ $] *?代替:
r'.*?\([^$]*?([\$|\d][^)]* per contract)\)'
#2
2
You could probably use a less specific regex
您可以使用不太具体的正则表达式
re.findall(r'\(([^)]+) per contract\)', str)
This will match the "$6.00" and the "10 Cents."
这将匹配“$ 6.00”和“10美分”。
#3
0
You can use:
您可以使用:
r'(?<=\()[^=][^)]*? per contract(?=\))'
#4
0
This will match the output you specified in your comments:
这将匹配您在评论中指定的输出:
re.search('\((([^)]+) per contract)\)', str).group(1)