Python:一个正则表达式,它忽略了带有特定单词的文本

2024-10-05 11:14:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下文本,存储在一个名为

info['growing_info']

Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

我想要一个正则表达式,它将返回。。。你知道吗

>>>seed_spacing.group(1)
>>>1
>>>seed_spacing.group(2)
>>>"

我目前的尝试是

seed_spacing = re.search(r'(?:Sow|sow|transplant|Transplant).+(?!rows).+([\d]+)(\'|") apart',info['growing_info'])

这不是我想要的结果。我知道松散使用点字符是有害的,但我不知道如何继续。你知道吗


Tags: andofininfowithanyseedabout
2条回答

删除\d周围的方括号

您可以使用以下正则表达式:

re.search(r'(?:sow|transplant).+?(\d+)(\'|") apart', info['growing_info'], re.I)

regex101 demo

我做了一些修改,即:

  • 添加了ignorecase标志以便可以使用(?:sow|transplant)
  • 删除了(?!rows).+;它没有做任何事情,并将.+更改为.+?以进行延迟匹配。你知道吗

如果要绝对避免在匹配的文本中使用rows,请使用以下命令:

re.search(r'(?:sow|transplant)(?:(?!rows).)+(\d+)(\'|") apart', info['growing_info'], re.I)

(?:(?!rows).)将匹配除rows之外的任何字符。你知道吗


.+(?!rows).+

不起作用,因为(?!rows)没有检查任何.+匹配的每个字符。下面是它的工作原理(粗体部分表示到目前为止匹配的内容):

  1. 正则表达式尝试匹配(?:sow|transplant)并找到它

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  2. 接下来是.+,这意味着匹配每个字符(贪婪地):

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  3. 接下来是(?!rows)。regex检查并发现最后没有rows

  4. 接下来是.+。正则表达式必须回溯1个字符才能允许此匹配:

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

    然后将最后一个字符与.+匹配

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  5. 下一个是(\d+),所以regex查找一个数字,但找不到。因为它不能回溯到第二个.+,所以它将回溯到第一个.+

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  6. 检查(?!rows)。一切都很清楚,因为前面没有rows。它将空格与第二个.+匹配,然后将数字与(\d+)匹配,然后将引号匹配。


(?:(?!rows).)

下面是它现在的工作原理:

  1. 正则表达式尝试匹配(?:sow|transplant)并找到它

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  2. 接下来是(?:(?!rows).),这意味着,匹配一个字符并确保每个字符前面没有rows(假设它与+匹配了两个字符,因为空格不能加粗):

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

    因为没有rows,所以继续重复这个组。持续到:

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  3. 现在它无法前进,因为前面有rows。因此,它继续处理regex的其他部分:(\d+)。因此,它可以回溯到:

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

  4. 也匹配(\d+)(\'|")。但是,没有apart要匹配,因此继续回溯,直到(\d+)(\'|") apart匹配为止:

    Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.

相关问题 更多 >

    热门问题