簡體   English   中英

如何找到文本中匹配單詞的索引?

[英]How to find the index of words matched in a text?

我正在提取與此正則表達式匹配的單詞的索引。 它使用正則表達式匹配文本中所有必需的單詞,但也匹配正則表達式的剩余空間。 它不限制左側文本中的匹配字符串,而是使用\\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\.?)
^^^^^^^^^^^^^^^^^^^^

正則表達式演示

(?:\\b(price|rs)\\s*)? pattern將匹配單詞邊界,后跟pricers ,后跟0+空格字符,整個模式將嘗試一次,並且由於? ,該模式是可選的 修飾符(圖案的整個序列可以匹配1或0次)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM