Python 使用边界时不基于正则表达式提取匹配的文本

Python re not extracting matched text based on regex while using boundary

我正在提取 this text from regex,我在文本中匹配了所需的字符串,但是在使用 python re 提取那些匹配的文本时,它没有提取。

这是我使用的代码。

import re
PRICE = '\b(price|rs)?\s*(\d+[\s\d.]*\s*?(pkg|k|m| 
(?:la(?:c|kh|k)|crore|cr)s?|l)\b\.?)'

content ='This should matchprice  5.6 lacincluding price(i.e  price 
5.6 lac) and rs 56 m. including rs (i.e rs 56 k  rs 56 m) .

It will match normally if there is no price or rs written for example 
or   56 k or 8.8 crs. are correct matching.

It should not match5.6  lac (Should not match eitherrs 6 lac asas 
there is no spaces before 5.6'

for m in re.finditer(PRICE,content,pat.FLAG):
    matched = m.group().strip()
    print ("In matched "+ matched)`

以上代码没有进入 for 循环。任何线索高度赞赏。谢谢

使用原始字符串定义正则表达式:

PRICE = r'\b(price|rs)?\s*(\d+[\s\d.]*\s*?(pkg|k|m|(?:la(?:c|kh|k)|crore|cr)s?|l)\b\.?)'

否则\b被解释为退格:

>>> print '\b(price|rs)?\s*(\d+[\s\d.]*\s*?(pkg|k|m|(?:la(?:c|kh|k)|crore|cr)s?|l)\b\.?)'
(price|rs)?\s*(\d+[\s\d.]*\s*?(pkg|k|m|(?:la(?:c|kh|k)|crore|cr)s?|l\.?)
>>> print r'\b(price|rs)?\s*(\d+[\s\d.]*\s*?(pkg|k|m|(?:la(?:c|kh|k)|crore|cr)s?|l)\b\.?)'
\b(price|rs)?\s*(\d+[\s\d.]*\s*?(pkg|k|m|(?:la(?:c|kh|k)|crore|cr)s?|l)\b\.?)

请注意第一个 print 输出如何不包含初始 \b。请记住,字符串首先由 python 编译器解释,这意味着所有常见的转义符,例如 \n 表示换行符或 \b 表示退格键或 \x42 表示 B 进行处理。然后将生成的字符串传递给 re 模块,该模块解释自己的转义符。因此,在 99.9% 的情况下,您希望避免编译器解释转义。原始字符串就是这样做的。

regex101 站点假定您使用的是原始字符串文字。