正则表达式强制执行特定模式
我想验证一个应该遵循特定定义模式的字符串。
规则是
- 它应该以“Dev”、“Tweak”或“Feature”这三个单词中的任何一个开头
- ,然后应该有一个空格连字符和空格,即
-
- 和三个或单词后跟一个句点。 (也可以有 URL 或“#”或标点符号)
所以我写了一个像这样的正则表达式,但有些它不起作用
^((Tweak|Dev|Feature)(\s-\s)(\w{3,})(\.))$
这里是正则表达式游乐场 URL: https://regex101.com/r/136LCG/1
正则表达式应匹配以下
- 字符串- 这应该是正确的。
- 功能 - 我的功能有一个 特殊字符如#123。
- 开发 - 这也应该有效 https://regex101.com/
- Dev - 这是我的消息。参考项目名称#123。
- Dev - 我的消息有很长的句子,带有额外的标点符号。
I want to validate a string which should follow a specific defined pattern.
Rule are
- it should starts with any of this three words 'Dev', 'Tweak' or 'Feature'
- then should have a space hyphen and space i.e.,
-
- and three or word followed by a period. (also it can have URL or '#' or punctuators)
So I have written a regex like this, but some how it is not working
^((Tweak|Dev|Feature)(\s-\s)(\w{3,})(\.))$
Here is the regex playground URL: https://regex101.com/r/136LCG/1
The regex should match following strings
- Tweak - this should be correct.
- Feature - my feature having a
special character as #123. - Dev - this should also work
https://regex101.com/ - Dev - this is my message. Ref projectname#123.
- Dev - my message having long sentence, with additional punctuators.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
regex demo 。 :
^
-字符串-
- 连字符(?:\ s+(?:#?\ w+| http \ s*))){3,},}
-更多重复\ s+
- 一个或多个Whitespaces(?:#?\ w+| http \ s*)
-#?\ w+
- 可选#
char,然后是一个或多个字母,数字或下划线|
- 或http \ s*
-http
,然后零或更多的非WhitespacesNote :Posixer不允许使用非捕捉组,并且根据您在何处的使用,可能无法使用
\ s
和\ \ \ code> \ w
shorthannd字符类。因此,在posix ere中,正则表达式看起来像详细信息:
(?:...)
()( ...)
\ s
变成[:space:]]
\ s
变成[^[^[[^[ :space:]]
\ w
变成[:alnum:] _]
You can use
See the regex demo. Details:
^
- start of string(?:Tweak|Dev|Feature)
- one of three words\s+
- one or more whitespaces-
- a hyphen(?:\s+(?:#?\w+|http\S*)){3,}
- three or more repetitions of\s+
- one or more whitespaces(?:#?\w+|http\S*)
- either of#?\w+
- an optional#
char and then one or more letters, digits or underscores|
- orhttp\S*
-http
and then zero or more non-whitespaces\.?
- an optional.
$
- end of string.NOTE: POSIX ERE does not allow using non-capturing groups, and depending on where you are using it, it might not be possible to use
\s
and\w
shorthannd character classes. So, in POSIX ERE, the regex will look likeDetails:
(?:...)
with(...)
\s
turned into[[:space:]]
\S
turned into[^[:space:]]
\w
turned into[[:alnum:]_]