我正在尝试编写一个执行以下操作的正则表达式:
- 查找至少一个 =
,并将这些 =
一直到 1) 行尾,或 2) 一个点 .
正则表达式:
[=]+?[=]+.*?[.$]+
测试字符串:
b == 123 //does not match, but which should as it is end of line!
b == 123. //does match "== 123.", which is OK
b == 123.abc //does match "== 123.", which is OK
我在 endofline $
anchor 中缺少什么?
最佳答案
[.$]
表示 character class由点或美元符号组成。如果您想要正则表达式元素之间的替代品 you should use |
,即 (\.|$)
。
此外,您可以使用 negative character classes [^…]
而不是 lazy matching ...*?
:
([^=]+)=+([^.]+)(?:\.|$)
关于java - 如何将正则表达式与行尾匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16234390/