正则表达式强制执行特定模式

发布于 2025-01-17 17:43:48 字数 828 浏览 2 评论 0原文

我想验证一个应该遵循特定定义模式的字符串。

规则是

  • 它应该以“Dev”、“Tweak”或“Feature”这三个单词中的任何一个开头
  • ,然后应该有一个空格连字符和空格,即 -
  • 和三个或单词后跟一个句点。 (也可以有 URL 或“#”或标点符号

rules img

所以我写了一个像这样的正则表达式,但有些它不起作用

^((Tweak|Dev|Feature)(\s-\s)(\w{3,})(\.))$

这里是正则表达式游乐场 URL: https://regex101.com/r/136LCG/1

正则表达式应匹配以下

  1. 字符串- 这应该是正确的。
  2. 功能 - 我的功能有一个 特殊字符如#123。
  3. 开发 - 这也应该有效 https://regex101.com/
  4. Dev - 这是我的消息。参考项目名称#123。
  5. 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)

rules img

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

  1. Tweak - this should be correct.
  2. Feature - my feature having a
    special character as #123.
  3. Dev - this should also work
    https://regex101.com/
  4. Dev - this is my message. Ref projectname#123.
  5. Dev - my message having long sentence, with additional punctuators.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2025-01-24 17:43:48

您可以使用

^(?:Tweak|Dev|Feature)\s+-(?:\s+(?:#?\w+|http\S*)){3,}\.?$

regex demo 。 :

  • 的开始
  • ^ -字符串
  • 详细信息 > - 一个或多个Whitespaces
  • - - 连字符
  • (?:\ s+(?:#?\ w+| http \ s*))){3,},} -更多重复
    • \ s+ - 一个或多个Whitespaces
    • (?:#?\ w+| http \ s*) -
      • #?\ w+ - 可选 char,然后是一个或多个字母,数字或下划线
      • | - 或
      • http \ s* - http,然后零或更多的非Whitespaces
  • ​ - 字符串的结尾。

Note :Posixer不允许使用非捕捉组,并且根据您在何处的使用,可能无法使用\ s\ \ \ code> \ w shorthannd字符类。因此,在posix ere中,正则表达式看起来像

^(Tweak|Dev|Feature)[[:space:]]+-([[:space:]]+(#?[[:alnum:]_]+|http[^[:space:]]*)){3,}\.?$

详细信息

  • 所有非捕捉组都被捕获的组替换为(?:...))( ...)
  • \ s变成[:space:]]
  • \ s变成[^[^[[^[ :space:]]
  • \ w变成[:alnum:] _]

You can use

^(?:Tweak|Dev|Feature)\s+-(?:\s+(?:#?\w+|http\S*)){3,}\.?$

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
      • | - or
      • http\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 like

^(Tweak|Dev|Feature)[[:space:]]+-([[:space:]]+(#?[[:alnum:]_]+|http[^[:space:]]*)){3,}\.?$

Details:

  • All non-capturing groups are replaced with capturing ones, (?:...) with (...)
  • \s turned into [[:space:]]
  • \S turned into [^[:space:]]
  • \w turned into [[:alnum:]_]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文