后视行为

Lookbehind behavior

为什么下面的回顾

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

return 任何匹配项:

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

尽管 return 匹配 (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

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

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

也许你可以不用回头看然后让 "my " 可选 (?:my )?:

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

"checking" 或 "savings" 或 "CD" 将在捕获组 1 中。