我有这个字符串。
string = """Horseradish CULTURE: Well-drained, friable soil with a pH range of 6.2-6.8 will yield the best results. When roots are received, work the soil about a foot deep and incorporate compost, manure, or fertilizer. Make a 5-6" deep furrow and plant root cuttings 12" apart, slanted 2-3" deep with the flat-cut end up..."""
和这段代码
seed_spacing = re.search(r'(?:sow|transplant|plant)(?:(?!rows).)+(\d+)(\'|") apart', string, re.I)
seed_spacing.group()
>>>Make a 5-6" deep furrow and plant root cuttings 12" apart
seed_spacing.group(1)
>>>2
我想看到 12,但我得到的是 2。对于一位数字的情况,我需要它灵活。我认为 + 是贪婪的。我错过了什么?
请您参考如下方法:
+ 是贪婪的 - 但它在 \d+ 中不只是贪婪,它在 中也是贪婪>(?:(?!rows).)+.后者正在吃掉 1。也许您更喜欢 (?:(?!rows)\D)+(即吃掉 不是 数字的字符)。
