Right way to catch slug1/slug2/slug3 page with Regex?

92 Views Asked by At

I am trying to use this Re-pattern r'\({2}.+?\){2}' to catch a ((slug1/slug2/slug3 someword)) expression from text.

It gives me the whole expression itself,i.e '((slug1/slug2/slug3 someword))'. Then I parse it using Python:split it to get slug1/slug2/slug3 and someword separately.

How can I get the same using pure Regex pattern with groups. What pattern should be? Any help is appreciated.

2

There are 2 best solutions below

0
On

Assuming slugs can't contain whitespace:

\({2}(\S*)\s(.*?)\){2}

More explicitly:

\({2}  # two literal '(' characters
(\S*)  # any number of non-whitespace characters, captured in group 1
\s     # any whitespace character
(.*?)  # any number of characters, reluctantly, captured in group 2
\){2}  # two literal ')' characters

So slug1/slug2/slug3 will be in group 1 and someword will be in group 2.

6
On

I came up with this regex:

/([\w\/]+) (\w+)/

It evaluates correctly using this command:

perl -e '$a="((slug1/slug2/slug3 someword))"; if ($a =~ /([\w\/]+) (\w+)/) {print "$1 $2"}'