Problem with a regex
I want a regex that will match:
A type with an ID:
[Image=4b5da003ee133e开发者_开发技巧8368000002]
[Video=679hfpam9v56dh800khfdd32]
With between 0 and n additional options separated with @
:
[Image=4b5da003ee133e8368000002@size:small]
[Image=4b5da003ee133e8368000002@size:small@media:true]
I have this so far :
\[[a-zA-Z]*=[a-zA-Z0-9]*[@[a-zA-Z]*:[a-zA-Z]*]*\]
... but it's not matching all the cases.
\[[a-zA-Z]+=[a-zA-Z0-9]{24}(@[a-zA-Z]+:[a-zA-Z]+)*\]
^ ^
You were enclosing that section with [], which as you are aware, is for a class, you just want a grouping. You should also ensure that the first match has at least one character, and it seems the id block has 24characters always, if this is the case use, {X} to define a repetition of length X.
Shouldn't the additional options be grouped (instead of brackets!) and marked optional (instead of *)? And you should use + instead of * or else an empty string would be matched.
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]*:[a-zA-Z]*)*\]
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)*\]
You need to enclose the optional group in parentheses, not brackets.
^\[\w+=\w+(@\w+:\w+)*\]$
I guess it should be possible to be more specific.
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)?\]
I think this will be little better :)
精彩评论