Lookbehind行为

时间:2018-02-18 06:10:31

标签: regex search java-8 lookbehind

为什么以下的后卫

(?<=to my\s)(checking|savings|CD) | (?<=to\s)(checking|savings|CD)

返回任何匹配:

Can you transfer from my CD 200 dollars to my checking, please?

尽管正确返回了匹配项(checking):

Can you transfer from my CD 200 dollars to checking, please?

以下情况适用于两种情况:

(?<=from my\s)(checking|savings|CD) | (?<=from\s)(checking|savings|CD)

Can you transfer from my CD 200 dollars to checking, please? --> CD

Can you transfer from CD 200 dollars to checking, please?  --> CD

我正在尝试制定一个正则表达式,它能够在上面的加号中解析出目的地[后跟'到']以及以下文本字符串(或类似):

- Transfer $20 from checking to savings please.
- Can you transfer from my CD 200 dollars to my checking, please?
- Please send from savings to checking 45.

1 个答案:

答案 0 :(得分:1)

也许你可以在没有外观的情况下做到这一点,然后让我的&#34;我的&#34;可选(?:my )?

to (?:my )?(checking|savings|CD)

&#34;检查&#34;或者&#34;储蓄&#34;或者&#34; CD&#34;将被捕获​​的第1组。