使用正则表达式确保有效字符串以指定的文字字符开头
我正在尝试使用正则表达式,但由于缺乏使用它们的经验,我正在努力。这个想法是扫描以 '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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会在第一个
^
之后添加 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.
/^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 ..."
只需添加“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})".
语句的开头是:
要记住的是像
[AZ]{3}
这样的语句会查找连续的任意 3 个大写字母,换句话说,它查找模式,而不是查找模式。与您想要的完全匹配。除非
GB:
之后有特定的内容需要查找,否则您可以将其缩短为^GB:.*$
。The start of the statement would be:
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:.*$
.您应该能够像大家所说的那样将
GB:
添加到前面,但是现有的正则表达式中存在错误。在自由间距模式下更容易看到:^
锚点仅影响第一个选项,而$
仅影响第三个选项。您必须添加另一层遏制:...现在您可以添加前缀:
...或在线路噪声模式下:
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:The
^
anchor only affects the first alternative, and the$
only affects the third one. You have to add another layer of containment:...and now you can add the prefix:
...or in line-noise mode: