tags:

views:

73

answers:

5

I want a regex that will match:

A type with an ID:

[Image=4b5da003ee133e8368000002]
[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.

+5  A: 
\[[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.

nlucaroni
thank you, that was the problem
marcgg
There is still one issue: if you do this, it won't get all the options, only the last one. In the example "[Image=4b5da003ee133e8368000002@size:small@media:true]" I can only retrieve "@media:true" when I also need size:true
marcgg
That's what the trailing * is for. if you want to group the WHOLE section into one variable, another pair of round brackets are required, and add a '?:' after the opening inner parens. This will avoid creating a backreference. The next match for you should be in the next variable in the current case.
nlucaroni
You should check the standards of ruby to see how it supports (?:data)
nlucaroni
@nlycaroni: I'm not sure what you mean. Let's say I want to get "size:small" and "media:true" in two separate variables, how should I proceed ? This is kind of outside the scope of this question but... :)
marcgg
I ended up opening another question: http://stackoverflow.com/questions/2230372/getting-some-elements-in-a-string-using-a-regex
marcgg
+3  A: 

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]*)*\]
AndiDog
Thanks you for the answer. nlucaroni was faster so he gets the check, but this is correct as well
marcgg
I'd expand on that further and make both the key and value required when an optional field is present, although the OP didn't account for this in the question. `\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)?\]`
Jimmy Cuadra
? is the wrong quantifier for (@[a-zA-Z]*:[a-zA-Z]*). There are between 0 to n additional options.
Erlock
@Erlock: Corrected.
AndiDog
+2  A: 
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)*\]

You need to enclose the optional group in parentheses, not brackets.

Tim Pietzcker
Like I said to andidog, nlucaroni was faster so he gets the check, but this is correct as well. Thanks :)
marcgg
A: 
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)?\]

I think this will be little better :)

tt.Kilew
+1  A: 
^\[\w+=\w+(@\w+:\w+)*\]$

I guess it should be possible to be more specific.

Erlock
Interesting solution but it would also include underscores, which I don't want
marcgg