正则表达式不以点开头或以点结尾 [英] Regex not start with dot or end with dot
本文介绍了正则表达式不以点开头或以点结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个不以点开头或以[-_.]
结尾的正则表达式.
I need a regular expression that does not start with a dot or end with [-_.]
.
这个正则表达式有效,但在第一个条件下失败;它不以点开头:
This regex works but fails for the first condition; it does not start with dot:
^[A-Za-z0-9][^.]*[^-_.][A-Za-z0-9]$
例如:test.com
应该是一个有效的字符串,但它失败了.
For example: test.com
should be a valid string but it fails.
推荐答案
从您的上一个问题,您应该能够使用:
From your previous question, you should be able to use:
^[^.].*[^-_.]$
但是如果您希望能够匹配 1 个字符的字符串,您将需要负前瞻:
But if you want to be able to match a 1 character string, you will need negative lookaheads:
^(?![.])(?!.*[-_.]$).+
如果您也想匹配空字符串,只需使用 *
而不是 +
.
And if you want to match empty strings too, simply use *
instead of +
.
^(?![.])(?!.*[-_.]$).*
这篇关于正则表达式不以点开头或以点结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文