如何匹配以下正则表达式python?

如何匹配以下正则表达式python?,python,regex,Python,Regex,如何将以下内容与正则表达式匹配 string1 = '1.0) The Ugly Duckling (TUD) (10 Dollars)' string2 = '1.0) Little 1 Red Riding Hood (9.50 Dollars)' 我正在尝试以下方法: groupsofmatches = re.match('(?P<booknumber>.*)\)([ \t]+)?(?P<item>.*)(\(.*\))?\(.*?((\d+)?(\.\d+)?)

如何将以下内容与正则表达式匹配

string1 = '1.0) The Ugly Duckling (TUD) (10 Dollars)'
string2 = '1.0) Little 1 Red Riding Hood (9.50 Dollars)'
我正在尝试以下方法:

groupsofmatches = re.match('(?P<booknumber>.*)\)([ \t]+)?(?P<item>.*)(\(.*\))?\(.*?((\d+)?(\.\d+)?).*([ \t]+)?Dollars(\))?', string1)

您可以对重复的字符施加更严格的限制:

groupsofmatches = re.match('([^)]*)\)[ \t]*(?P<item>.*)\([^)]*?(?P<dollaramount>(?:\d+)?(?:\.\d+)?)[^)]*\)$', string1)
这将确保数字取自最后一组括号。

我将其写成:

num, name, value = re.match(r'(.+?)\) (.*?) \(([\d.]+) Dollars\)', s2).groups()
您的问题是,.*非常匹配,并且可能占用了太多的字符串。打印所有匹配组将使这一点更加明显:

import re

string1 = '1.0) The Ugly Duckling (TUD) (10 Dollars)'
string2 = '1.0) Little 1 Red Riding Hood (9.50 Dollars)'

result = re.match(r'(.*?)\)([ \t]+)?(?P<item>.*)\(.*?(?P<dollaramount>(\d+)?(\.\d+)?).*([ \t]+)?Dollars(\))?', string1)

print repr(result.groups())
print result.group('item')
print result.group('dollaramount')

这在某些RE引擎中可能会很昂贵,因此您也可以编写eg\[^]*\来匹配所有括号。如果你没有处理大量的文本,这可能没关系

顺便说一句,您真的应该为regexp使用原始字符串即r'something',以避免令人惊讶的反斜杠行为,并为读者提供线索


我知道你有这个小组\.*?\?这大概是删掉了TUD,但如果你真的想在标题中删掉它,就把它删掉。

这就是我用一个


?p\d+?:\。\d+?\\s+?p.*\s+\\d+?:\。\d+?\s+美元\

我建议您使用正则表达式模式

(?P<booknumber>[^)]*)\)\s+(?P<item>.*\S)\s+\((?!.*\()(?P<amount>\S+)\s+Dollars?\)

是否应该捕获TUD?TUD应该作为名称、更新帖子的一部分捕获。您所说的名称是指项目?
(?P<booknumber>[^)]*)\)\s+(?P<item>.*\S)\s+\((?!.*\()(?P<amount>\S+)\s+Dollars?\)