2

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.

1

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

5 Comments

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
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.
You should check the standards of ruby to see how it supports (?:data)
@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... :)
I ended up opening another question: stackoverflow.com/questions/2230372/…
3

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]*)*\]

3 Comments

Thanks you for the answer. nlucaroni was faster so he gets the check, but this is correct as well
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]+)?\]
? is the wrong quantifier for (@[a-zA-Z]*:[a-zA-Z]*). There are between 0 to n additional options.
2
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)*\]

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

1 Comment

Like I said to andidog, nlucaroni was faster so he gets the check, but this is correct as well. Thanks :)
1
^\[\w+=\w+(@\w+:\w+)*\]$

I guess it should be possible to be more specific.

1 Comment

Interesting solution but it would also include underscores, which I don't want
0
\[[a-zA-Z]+=[a-zA-Z0-9]+(@[a-zA-Z]+:[a-zA-Z]+)?\]

I think this will be little better :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.