使用正则表达式确保有效字符串以指定的文字字符开头

发布于 2024-11-29 08:59:49 字数 466 浏览 1 评论 0原文

我正在尝试使用正则表达式,但由于缺乏使用它们的经验,我正在努力。这个想法是扫描以 'GB:' 开头的特定字符串,

例如它应该检测:

  • GB:AB12ABC
  • GB:AB34 ABC

但不是:

  • US:AB12ABC
  • AB12ABC

我有这个与我正在查找的字符串匹配的正则表达式(考虑到不同的空间、格式等):

<代码>/^([AZ]{3}\s?(\d{3}|\d{2}|d{1})\s?[AZ])|([AZ]\s?(\ d{3}|\d{2}|\d{1})\s?[AZ]{ 3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s? [A-HJ-PR-Z]{3})$/

但现在我想在前面添加 GB: 位。为了做到这一点,我需要改变上面的表达式什么?

I'm trying to get a regular expression to work but I'm struggling due to my lack of experience with them. The idea is to scan for particular strings that begin with 'GB:'

For example it should detect:

  • GB:AB12ABC
  • GB:AB34 ABC

But not:

  • US:AB12ABC
  • AB12ABC

I have this regular expression that matches the strings I'm looking for (takes into account different spaces, formats etc):

/^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$/

But now I want to add the GB: bit on the front. What would I alter in the expression above to do this?

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

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

发布评论

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

评论(5

短叹 2024-12-06 08:59:49

我会在第一个 ^ 之后添加 GB:,因为这表示行的开头。

/^GB:([AZ]{3}\s?(\d{3}|\d{2}|d{1})\s?[AZ])|([AZ]\s?(\d {3}|\d{2}|\d{1})\s?[AZ ]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\ s?[A-HJ-PR-Z]{3})$/

编辑:是的,我想那里有一个:。对-o。

I would add a GB: after the first ^, since that's what denotes the beginning of a line.

/^GB:([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$/

Edit: yeah, I suppose there is a : there. Right-o.

梦行七里 2024-12-06 08:59:49

/^GB:([AZ]{3}\s?(\d{3}|\d{2}|d{1})\s?[AZ])|([AZ]\s? (\d{3}|\d{2}|\d{1})\s?[AZ] {3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s ?[A-HJ-PR-Z]{3})$/

只需让你的正则表达式说“以 GB 开头:然后......”

/^GB:([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$/

Just make your regex say "that starts with GB: and then ..."

谈场末日恋爱 2024-12-06 08:59:49

只需添加“GB:”,顺便说一下,您可以将表达式“(\d{3}|\d{2}|d{1})”简化为“(\d{1, 3})”。

Just add "GB:", by the way you can reduce your expression: "(\d{3}|\d{2}|d{1})" with simply "(\d{1, 3})".

此生挚爱伱 2024-12-06 08:59:49

语句的开头是:

^GB:([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$

要记住的是像 [AZ]{3} 这样的语句会查找连续的任意 3 个大写字母,换句话说,它查找模式,而不是查找模式。与您想要的完全匹配。

除非 GB: 之后有特定的内容需要查找,否则您可以将其缩短为 ^GB:.*$

The start of the statement would be:

^GB:([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$

The thing to remember is a statement like [A-Z]{3} looks for any 3 capital letters in a row, in other words, its looking for a pattern, not an exact match like you wanted.

Unless there is soemthing specific to look for after GB:, you could shorten it to ^GB:.*$.

两相知 2024-12-06 08:59:49

应该能够像大家所说的那样将GB:添加到前面,但是现有的正则表达式中存在错误。在自由间距模式下更容易看到:

/^([A-Z]{3} \s? \d{1,3} \s? [A-Z])
  |
  ([A-Z] \s? \d{1,3} \s? [A-Z]{3})
  |
  ([A-HK-PRSVWY][A-HJ-PR-Y] \s? (?:0[2-9]|[1-9][0-9]) \s? [A-HJ-PR-Z]{3})$
/x

^ 锚点仅影响第一个选项,而 $ 仅影响第三个选项。您必须添加另一层遏制:

/^
 (?:
   ([A-Z]{3} \s? \d{1,3} \s? [A-Z])
   |
   ([A-Z]\s? \d{1,3} \s? [A-Z]{3})
   |
   ([A-HK-PRSVWY][A-HJ-PR-Y] \s? (?:0[2-9]|[1-9][0-9]) \s? [A-HJ-PR-Z]{3})
 )$
/x

...现在您可以添加前缀:

/^
 GB:
 (?:
   ([A-Z]{3} \s? \d{1,3} \s? [A-Z])
   |
   ([A-Z]\s? \d{1,3} \s? [A-Z]{3})
   |
   ([A-HK-PRSVWY][A-HJ-PR-Y] \s? (?:0[2-9]|[1-9][0-9]) \s? [A-HJ-PR-Z]{3})
 )$
/x

...或在线路噪声模式下:

/^GB:(?:([A-Z]{3}\s?\d{1,3}\s?[A-Z])|([A-Z]\s?\d{1,3}\s?[A-Z]{3})|([A-HK-PRSVWY][A-HJ-PR-Y]\s?(?:0[2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3}))$/

You should be able to tack GB: onto the front like everyone says, but there's an error in the existing regex. It's easier to see in free-spacing mode:

/^([A-Z]{3} \s? \d{1,3} \s? [A-Z])
  |
  ([A-Z] \s? \d{1,3} \s? [A-Z]{3})
  |
  ([A-HK-PRSVWY][A-HJ-PR-Y] \s? (?:0[2-9]|[1-9][0-9]) \s? [A-HJ-PR-Z]{3})$
/x

The ^ anchor only affects the first alternative, and the $ only affects the third one. You have to add another layer of containment:

/^
 (?:
   ([A-Z]{3} \s? \d{1,3} \s? [A-Z])
   |
   ([A-Z]\s? \d{1,3} \s? [A-Z]{3})
   |
   ([A-HK-PRSVWY][A-HJ-PR-Y] \s? (?:0[2-9]|[1-9][0-9]) \s? [A-HJ-PR-Z]{3})
 )$
/x

...and now you can add the prefix:

/^
 GB:
 (?:
   ([A-Z]{3} \s? \d{1,3} \s? [A-Z])
   |
   ([A-Z]\s? \d{1,3} \s? [A-Z]{3})
   |
   ([A-HK-PRSVWY][A-HJ-PR-Y] \s? (?:0[2-9]|[1-9][0-9]) \s? [A-HJ-PR-Z]{3})
 )$
/x

...or in line-noise mode:

/^GB:(?:([A-Z]{3}\s?\d{1,3}\s?[A-Z])|([A-Z]\s?\d{1,3}\s?[A-Z]{3})|([A-HK-PRSVWY][A-HJ-PR-Y]\s?(?:0[2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3}))$/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文