I'm looking for a regex that will match a period character, ONLY if none of that period's surrounding characters are also periods.
我正在寻找一个与句号字符匹配的正则表达式,前提是那个时期的所有字符都不是句点。
Fine by me... leave! FAIL
Okay.. You win. SUCCEED
Okay. SUCCEED //Note here, the period is the last char in the string.
I was thinking do:
我想做的事:
[^\\.*]\\.
But that is just wrong and probably not at all in the right direction. I hope this question helps others in the same situation as well.
但这是错误的,很可能根本就不是正确的方向。我希望这个问题也能帮助同样处境的人。
Thanks.
谢谢。
3 个解决方案
#1
3
You need to wrap the dot in negative look arounds:
你需要把圆点包装成负面的外观:
(?<![.])[.](?![.])
I prefer [.]
over \\.
, because:
我更喜欢[。在\ \]。,因为:
- It's easier to read - there are too many back slashes in java literals already
- 它更容易阅读——java文字中已经有太多的反斜杠。
-
[.]
looks a bit like an X wing fighter from Star Wars ™ - (。看上去有点像一个X翼战斗机从星球大战™
#2
0
You can use negative look ahead and look behind or this alternative regex:
你可以用消极的眼光看前面,看看后面或者这个替代的regex:
String regex = "(^\\.[^\\.]|[^\\.]\\.[^\\.]|[^\\.]\\.$)";
The first alternative check the beginning ^
of the string (if it can start with a dot), the second looks for any dot inside and the third looks for a dot at the end of the string $
.
第一个替代检查字符串的开始^(如果它可以从一个点开始),第二个查找任何点在第三个查找字符串的最后一个点美元。
#3
0
That regex will still match any period that isn't preceded by another period.
该regex仍将匹配任何不包含另一个周期的周期。
[^\.]\.[^\.]
Takes care of both sides of the target period.
[^ \]\[^ \。在目标周期内兼顾双方。
EDIT: Java doesn't have a raw string like Python, so you would need full escapes: [^.]\\.[^.]|^\\.[^.]|[^.]\\.$
编辑:Java没有原始字符串像Python,所以你需要完全逃脱:[^]\ \[^]。| ^ \ \[^]。|[^]\ \ $
#1
3
You need to wrap the dot in negative look arounds:
你需要把圆点包装成负面的外观:
(?<![.])[.](?![.])
I prefer [.]
over \\.
, because:
我更喜欢[。在\ \]。,因为:
- It's easier to read - there are too many back slashes in java literals already
- 它更容易阅读——java文字中已经有太多的反斜杠。
-
[.]
looks a bit like an X wing fighter from Star Wars ™ - (。看上去有点像一个X翼战斗机从星球大战™
#2
0
You can use negative look ahead and look behind or this alternative regex:
你可以用消极的眼光看前面,看看后面或者这个替代的regex:
String regex = "(^\\.[^\\.]|[^\\.]\\.[^\\.]|[^\\.]\\.$)";
The first alternative check the beginning ^
of the string (if it can start with a dot), the second looks for any dot inside and the third looks for a dot at the end of the string $
.
第一个替代检查字符串的开始^(如果它可以从一个点开始),第二个查找任何点在第三个查找字符串的最后一个点美元。
#3
0
That regex will still match any period that isn't preceded by another period.
该regex仍将匹配任何不包含另一个周期的周期。
[^\.]\.[^\.]
Takes care of both sides of the target period.
[^ \]\[^ \。在目标周期内兼顾双方。
EDIT: Java doesn't have a raw string like Python, so you would need full escapes: [^.]\\.[^.]|^\\.[^.]|[^.]\\.$
编辑:Java没有原始字符串像Python,所以你需要完全逃脱:[^]\ \[^]。| ^ \ \[^]。|[^]\ \ $