正则识别特定模式

发布于 2025-01-20 22:01:28 字数 454 浏览 4 评论 0原文

我正在编写正则是我的字符串中的特定模式。我必须确定字符串是否满足我想要的所有模式。我有以下标准:

  1. 该名称应以“ p”或“ q”或“ r”开始
  2. ,之后的第一个字符应匹配字符串,
  3. 如果存在xyz,则应 匹配“ xyz”或“ abcd” “ H”或“ D”,如果存在“ ABCD”,则第9个字符应为“ H”或“ D”。

字符串可能是:

PXYZ****H***** -> Should be true
QABCD****H***** -> Should be true
AXYG****Z***** -> Should be false
RABCD****H=D***** -> Should be true

如果字符串以开头([P | Q | r])\ W+,我已经尝试过,不确定如何结合其他。

I am writing regex to find a specific pattern in my string. I have to identify if the string satisfy all the pattern that I am looking for. I have following criteria:

  1. The name should start with either "P" or "Q" or "R"
  2. Following the first character the string should match either "XYZ" or "ABCD"
  3. If the XYZ is present then the 8th character should either be "H" or "D", if "ABCD" is present the 9th character should be either "H" or "D".

String could be:

PXYZ****H***** -> Should be true
QABCD****H***** -> Should be true
AXYG****Z***** -> Should be false
RABCD****H=D***** -> Should be true

I have tried if the string starts with ([P|Q|R])\w+, not sure how to combine others.

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

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

发布评论

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

评论(2

冰魂雪魄 2025-01-27 22:01:28

使用

^[PQR](XYZ|ABCD)....[HD].*

请参阅正则表达式证明

说明

^ asserts position at start of a line
 Match a single character present in the list below [PQR]
   PQR matches a single character in the list PQR (case sensitive)
 1st Capturing Group (XYZ.|ABCD.)
   1st Alternative XYZ.
     XYZ matches the characters XYZ literally (case sensitive)
 2nd Alternative ABCD.
   ABCD matches the characters ABCD literally (case sensitive)

 . matches any character (except for line terminators)
 . matches any character (except for line terminators)
 . matches any character (except for line terminators)
 . matches any character (except for line terminators)
 Match a single character present in the list below [HD]
   HD matches a single character in the list HD (case sensitive)
 . matches any character (except for line terminators)
 * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Use

^[PQR](XYZ|ABCD)....[HD].*

See regex proof.

EXPLANATION

^ asserts position at start of a line
 Match a single character present in the list below [PQR]
   PQR matches a single character in the list PQR (case sensitive)
 1st Capturing Group (XYZ.|ABCD.)
   1st Alternative XYZ.
     XYZ matches the characters XYZ literally (case sensitive)
 2nd Alternative ABCD.
   ABCD matches the characters ABCD literally (case sensitive)

 . matches any character (except for line terminators)
 . matches any character (except for line terminators)
 . matches any character (except for line terminators)
 . matches any character (except for line terminators)
 Match a single character present in the list below [HD]
   HD matches a single character in the list HD (case sensitive)
 . matches any character (except for line terminators)
 * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
黯淡〆 2025-01-27 22:01:28

此正则具体的是:

  • pqr
  • 开始使用xyzabcd
  • 具有h或<代码> d 结束前的五个字符

是我的尝试:

'^[PQR](XYZ|ABCD).*[HD].{5}

它对您有用吗?

它对您有用吗?

What is specific about this regex is that:

  • starts with PQR
  • continues with XYZ or ABCD
  • has an H or D five chars before the end

Here's my attempt:

'^[PQR](XYZ|ABCD).*[HD].{5}

Does it work for you?

Does it work for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文