kopacabana
kopacabana

Reputation: 467

RegExp recursive

I try to implement a recursion with a regexp to match all my items but that's not work.

Here is my regexp : ({)(\n([a-z-]):([a-zA-Z0-9#.'"()])\;)*(\n}) And the string :

{
fill-color:#555;
fill-opacity:0.15;
fill-blend:'multiply';
}
{
stroke-color:#f90;
stroke-width:5pt;
stroke-opacity:0.2;
}
{
stroke-color:#f00;
stroke-width:0.5pt;
}
{
stroke-color:#f00;
}
]

And the link on reg101 to test : https://regex101.com/r/0dLyT4/2

With this regexp, I've got only the last iteration.

match1 > fill-blend:'multiply';
match2 > stroke-opacity:0.2;
match3 > stroke-width:0.5pt;
match4 > stroke-color:#f00;

What must I change to match :

match1 > fill-color:#555;
match2 > fill-opacity:0.15;
match3 > fill-blend:'multiply';
match4 > stroke-color:#f90;
match5 > stroke-width:5pt;
match6 > stroke-opacity:0.2;
match7 > stroke-color:#f00;
match8 > stroke-width:0.5pt;
match9 > stroke-color:#f00;

Thanks for your time !

Upvotes: 0

Views: 109

Answers (4)

CoderX
CoderX

Reputation: 302

The following regex is matching your criteria.

(?m)^\s*\w+[^:]+:[^;\n]+;

Here is the link https://regex101.com/r/Sy7nmh/1

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163207

You could use a single capturing group and repeat a non capturing group (?:...)* inside it. Repeat the non capturing group 1+ times to prevent getting empty matches.

Using this pattern [a-z\-]*):([a-zA-Z0-9#.'"()]* with a * multiplier would also allow a single : to be matched.

Note that you don't have to escape the \- as it is the last char in the character class and the \; does not need escaping by itself.

\{\n((?:[a-z-]+:[a-zA-Z0-9#.'"()]+;\n)+)}

Regex demo

If you also don't want to capture the ending newline, you could repeat the non capturing group 0+ times and match at least a single item where the last newline will be outside of the capturing group.

\{\n((?:[a-z-]*:[a-zA-Z0-9#.'"()]+;\n)*[a-z-]*:[a-zA-Z0-9#.'"()]+;)\n}

Regex demo

Upvotes: 1

Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6390

You can try this regex. This will match all the CSS properties at group1.

{(\n+([a-z0-9#\.\-:'"]+;\n*)*)}

Here is regex101 link

Upvotes: 0

Guerric P
Guerric P

Reputation: 31805

As regex101.com says:

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data

Here is your regex fixed with a surrounding capture group: https://regex101.com/r/C7ANXp/1. What you need is in the group 2.

Upvotes: 0

Related Questions