为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕容708150 2021-06-07 02:18:44
我正在提取与此正则表达式匹配的单词索引。它使用正则表达式匹配文本中所有必需的单词,但它也匹配正则表达式左侧的空格。它不是限制左侧文本中的匹配字符串,而是使用 限制匹配字符串的右侧\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.6How can I modify above regex to bound the matched word in the left side as well? 
查看完整描述

1 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

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

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

查看正则表达式演示

(?:\b(price|rs)\s*)?模式会匹配字边界,随后与pricers所遵循与0+空格字符,整个图案将曾经尝试,并且图案是可选的,由于?改性剂(图案的整个序列可以匹配1或0次)


查看完整回答
反对 回复 2021-06-09
  • 1 回答
  • 0 关注
  • 134 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号