I am trying to match the following pattern.
A minimum of 3 'groups' of alphanumeric characters separated by a hyphen.
Eg: ABC1-AB-B5-ABC1
Each group can be any number of characters long.
I have tried the following:
^(\w*(-)){3,}?$
This gives me what I want to an extent.
ABC1-AB-B5-0001 fails, and ABC1-AB-B5-0001- passes.
I don't want the trailing hyphen to be a requirement.
I can't figure out how to modify the expression.
Your ^(\\w*(-)){3,}?$
pattern even allows a string like -----
because the only required pattern here is a hyphen: \\w*
may match 0 word chars. The -
may be both leading and trailing because of that.
You may use
\A\w+(?:-\w+){2,}\z
Details :
\\A
- start of string \\w+
- 1+ word chars (that is, letters, digits or _
symbols) (?:-\\w+){2,}
- 2 or more sequences of:
-
- a single hyphen \\w+
- 1 or more word chars \\z
- the very end of string. See the regex demo .
Or, if you do not want to allow _
:
\A[^\W_]+(?:-[^\W_]+){2,}\z
or to only allow ASCII letters and digits:
\A[A-Za-z0-9]+(?:-[A-Za-z0-9]+){2,}\z
可能是这样的:
^\w+-\w+-\w+(-\w+)*$
^(\w+-){2,}(\w+)-?$
匹配2个以上由连字符分隔的组,然后匹配一个可能由连字符终止的组。
((?:-?\\w+){3,})
Matches minimum 3 groups, optionally starting with a hyphen, thus ignoring the trailing hyphen.
Note that the \\w
word character also select the underscore char _
as well as 0-9
and az
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.