python - 如何找到文本中匹配的单词索引?

标签 python regex regex-group

我正在提取 this regex 中匹配的单词索引。它使用正则表达式匹配文本中所有必需的单词,但也匹配正则表达式左侧的空格。它不是在左侧文本中限制匹配的字符串,而是使用 \b

限制匹配字符串的右侧

正则表达式:

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

输入文字:

    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 but its should bound the matched string from left side as well just like its not matching sapce after end of the matched string.

It should not match the spaces left of 8.5 in this      8.5 lac ould not match eitherrs 6 lac asas there is no spaces before 5.6

How can I modify above regex to bound the matched word in the left side as well? 

最佳答案

您可以将 \s* 移动到可选的非捕获组中:

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

请参阅regex demo

(?:\b(price|rs)\s*)? 模式将匹配单词边界,后跟 pricers 后跟 0+ 空白字符,整个模式将被尝试一次,并且由于 ? 修饰符,该模式是可选的(整个模式序列可以匹配 1 次或 0 次)

关于python - 如何找到文本中匹配的单词索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52148437/

相关文章:

python - 使用组标签 ?P<> 正则表达式到字典

python - r'\U' 给出 SyntaxError : (unicode error) 'rawunicodeescape' codec can't decode bytes

python - 如何从peewee获取sql查询?

Python/Django - 无法访问通用 View 中的链接模型

正则表达式替换不按预期删除空格

正则表达式仅覆盖所有匹配项中的第一个匹配项

regex - 正则表达式处理名称时出现未知问题

python ssl问题打开证书文件

php - 正则表达式语法中的 "OR"运算符

iphone - 使用 NSRegularExpression 提取文本