我有一个脚本,它接受用户输入的字符串。我想检查字符串输入是否应该恰好有 2 个点。相关性仅与点有关。字符串不应以点开头和结尾。不应有连续的点。
这是我正在使用的模式:
^[^\.]*\.[^\.]*\.[^\.]*$
Run Code Online (Sandbox Code Playgroud)
这是我正在寻找的字符串:
abc.def.xyz
Run Code Online (Sandbox Code Playgroud)
但在上面的模式中,如果点在前面或末尾,那么该字符串就会被选中 - 这是我不想要的。字符串中应该只有两个点。
不想要的:
.abc.xyz # no dot at the start
abc.xyz. # no dot at the end
abc.def.ced.xyz # only two dots not more than that
Run Code Online (Sandbox Code Playgroud)
我一开始就尝试使用(?!\.)
for 点,但没有成功。