Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with a regex

Tags:

regex

ruby

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.

like image 661
marcgg Avatar asked Apr 02 '26 03:04

marcgg


2 Answers

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

like image 183
nlucaroni Avatar answered Apr 03 '26 17:04

nlucaroni


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]*)*\]
like image 24
AndiDog Avatar answered Apr 03 '26 18:04

AndiDog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!