I'm working on a registration script for my client's product sales website.
我正在为客户的产品销售网站编写注册脚本。
I'm currently working on a reference ID input area, and I want to make sure that the reference ID is within the correct parameters of the payment method
我目前正在处理参考ID输入区域,我想确保参考ID在付款方式的正确参数范围内
The Reference ID will look something like this: XXXXX-XXXXX-XXXXX
参考ID将如下所示:XXXXX-XXXXX-XXXXX
I'm trying to use this RegEx pattern to match it: /(\w+){5}-(\w+){5}-(\w+){5}/
我正在尝试使用此RegEx模式来匹配它:/(\ w +){5} - (\ w +){5} - (\ w +){5} /
This matches it perfectly, but it also matches XXXXX-XXXXX-XXXXXXXXXX
这完全匹配,但它也匹配XXXXX-XXXXX-XXXXXXXXXX
Or at least it finds a match in there. I want it to make sure the entire string matches. I'm not too familiar with RegEx
或者至少它在那里找到匹配。我希望它确保整个字符串匹配。我对RegEx不太熟悉
How can I do this?
我怎样才能做到这一点?
9 个解决方案
#1
9
You need to use start and finish anchors. Alternatively, if you don't need to capture those groups, you can omit the parenthesis.
您需要使用开始和结束锚点。或者,如果您不需要捕获这些组,则可以省略括号。
Also, the +{5}
means match more than once exactly 5 times. I believe you didn't want that so I dropped the +
.
此外,+ {5}表示不止一次匹配5次。我相信你不想这样,所以我放弃了+。
/^\w{5}-\w{5}-\w{5}\z/
Also, I used \z
so your string doesn't match "abcde-12345-edcba\n"
.
另外,我使用\ z所以你的字符串与“abcde-12345-edcba \ n”不匹配。
#2
9
Use ^
and $
to match the start and end of the input string, respectively.
使用^和$分别匹配输入字符串的开头和结尾。
Also note that your use of +
was superfluous, as (\w+){5}
means "a word character, at least once, times five" which means it can match at least five times. You probably meant (\w){5}
(or just \w{5}
if you don't need the backreference; I'll assume in my example that you do).
另请注意,您使用+是多余的,因为(\ w +){5}表示“一个单词字符,至少一次,五次”,这意味着它可以匹配至少五次。您可能意味着(\ w){5}(如果您不需要反向引用,则只需要\ w {5};我将在我的示例中假设您这样做)。
/^(\w){5}-(\w){5}-(\w){5}$/
#3
2
put the regular expression in between ^
and $
to match the whole string and check if it matches anything
将正则表达式放在^和$之间以匹配整个字符串并检查它是否匹配任何内容
example:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
#4
1
Try
/^([\w]{5,5})-([\w]{5,5})-([\w]{5,5})$/i
There are several online regex tester out there, I work with this one before I code.
那里有几个在线正则表达式测试器,我在编码之前使用这个。
#5
0
Enclose it in "^" and "$" thus:
将它括在“^”和“$”中,这样:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
#6
0
You need ^
to match the start of the string and $
to match the end:
你需要^匹配字符串的开头,$匹配结束:
/^\w{5}-\w{5}-\w{5}$/
Note that (\w+){5}
is incorrect because that means five repetitions of \w+
, but that in turn means "one or more word characters".
请注意,(\ w +){5}不正确,因为这意味着重复5次\ w +,但这又意味着“一个或多个单词字符”。
#7
0
/^(\w){5}-(\w){5}-(\w){5}$/
You need to explicitly say that you want the pattern to start at the beginning of the string and end at it's ending.
You can improve it: /^((\w){5}-){2}(\w){5}$/
; this way, you can easily modify the number of elements your serial number might have.
/ ^(\ w){5} - (\ w){5} - (\ w){5} $ /你需要明确地说你希望模式从字符串的开头开始并在结束时结束。你可以改进它:/ ^((\ w){5} - ){2}(\ w){5} $ /;这样,您可以轻松修改序列号可能具有的元素数量。
#8
0
Use ^ and $ to mark the start and end of the regex string:
使用^和$标记正则表达式字符串的开头和结尾:
/^\w{5}-\w{5}-\w{5}$/
#9
-1
In preg, \b marks word boundaries. So you could try with something like
在preg中,\ b标记单词边界。所以你可以尝试类似的东西
/\b(\w+){5}-(\w+){5}-(\w+){5}\b/
#1
9
You need to use start and finish anchors. Alternatively, if you don't need to capture those groups, you can omit the parenthesis.
您需要使用开始和结束锚点。或者,如果您不需要捕获这些组,则可以省略括号。
Also, the +{5}
means match more than once exactly 5 times. I believe you didn't want that so I dropped the +
.
此外,+ {5}表示不止一次匹配5次。我相信你不想这样,所以我放弃了+。
/^\w{5}-\w{5}-\w{5}\z/
Also, I used \z
so your string doesn't match "abcde-12345-edcba\n"
.
另外,我使用\ z所以你的字符串与“abcde-12345-edcba \ n”不匹配。
#2
9
Use ^
and $
to match the start and end of the input string, respectively.
使用^和$分别匹配输入字符串的开头和结尾。
Also note that your use of +
was superfluous, as (\w+){5}
means "a word character, at least once, times five" which means it can match at least five times. You probably meant (\w){5}
(or just \w{5}
if you don't need the backreference; I'll assume in my example that you do).
另请注意,您使用+是多余的,因为(\ w +){5}表示“一个单词字符,至少一次,五次”,这意味着它可以匹配至少五次。您可能意味着(\ w){5}(如果您不需要反向引用,则只需要\ w {5};我将在我的示例中假设您这样做)。
/^(\w){5}-(\w){5}-(\w){5}$/
#3
2
put the regular expression in between ^
and $
to match the whole string and check if it matches anything
将正则表达式放在^和$之间以匹配整个字符串并检查它是否匹配任何内容
example:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
#4
1
Try
/^([\w]{5,5})-([\w]{5,5})-([\w]{5,5})$/i
There are several online regex tester out there, I work with this one before I code.
那里有几个在线正则表达式测试器,我在编码之前使用这个。
#5
0
Enclose it in "^" and "$" thus:
将它括在“^”和“$”中,这样:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
#6
0
You need ^
to match the start of the string and $
to match the end:
你需要^匹配字符串的开头,$匹配结束:
/^\w{5}-\w{5}-\w{5}$/
Note that (\w+){5}
is incorrect because that means five repetitions of \w+
, but that in turn means "one or more word characters".
请注意,(\ w +){5}不正确,因为这意味着重复5次\ w +,但这又意味着“一个或多个单词字符”。
#7
0
/^(\w){5}-(\w){5}-(\w){5}$/
You need to explicitly say that you want the pattern to start at the beginning of the string and end at it's ending.
You can improve it: /^((\w){5}-){2}(\w){5}$/
; this way, you can easily modify the number of elements your serial number might have.
/ ^(\ w){5} - (\ w){5} - (\ w){5} $ /你需要明确地说你希望模式从字符串的开头开始并在结束时结束。你可以改进它:/ ^((\ w){5} - ){2}(\ w){5} $ /;这样,您可以轻松修改序列号可能具有的元素数量。
#8
0
Use ^ and $ to mark the start and end of the regex string:
使用^和$标记正则表达式字符串的开头和结尾:
/^\w{5}-\w{5}-\w{5}$/
#9
-1
In preg, \b marks word boundaries. So you could try with something like
在preg中,\ b标记单词边界。所以你可以尝试类似的东西
/\b(\w+){5}-(\w+){5}-(\w+){5}\b/