您的位置:首页 > 编程语言 > Java开发

Java regex Lookaround

2013-10-22 01:39 387 查看
see: http://www.rexegg.com/regex-lookarounds.html
Four ways of using lookarounds:

(?= lookahead

(?=\d{3} dollars).{3}
(Lookahead). Looks ahead for three digits followed by " dollars". Matches "100" in "100 dollars"

(?! negative lookahead

(?!=\d{3} pesos)\d{3}
(Negative Lookahead). Makes sure what follows is not three digits followed by " pesos". Matches "100" in "100 dollars"

(?<= lookbehind

(?<=USD)\d{3}
(Lookbehind). Makes sure "USD" precedes the text to be matched. Matches "100" in "USD100"

(?<! negative lookbehind

(?<!USD)\d{3}
(Negative Lookbehind). Makes sure "USD" does not precede the text to be matched. Matches "100" in "JPY100"

Lookaround after match:

\d{3}(?= dollars)
(Lookahead). Makes sure " dollars" follows the three digits to be matched. Matches "100" in "100 dollars"

\d{3}(?! dollars)
(Negative Lookahead) Makes sure " dollars" does not follow the three digits to be matched. Matches "100" in "100 pesos"

.{3}(?<=USD\d{3})
(Lookbehind). Looks behind for "USD" followed by three digits. Matches "100" in "USD100"

\d{3}(?<!USD\d{3})
(Negative Lookbehind). Makes sure what precedes is not "USD" followed by three digits. Matches "100" in "JPY100"

n

n

n

n
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: