Skip to content
Advertisement

Can this do-able using regular expressions patterns (capture groups)?

In JS, I have a set of strings that need to break down into an array. Each string can be breakdown up to 5 groups (Some have less). I’m using regex to break all.

In the below string sets if it includes aa bb orcc it needs to go into the capture group2.

Im not very familiar with regex unfortunately and stuck at this point.

Is this pattern doable only using regex or do I have to break the from :: to | and explode it later?

My regex pattern ^[ t]*(?:(?:(w+)|)?(?:(w+):(?:(w+):)?)?)?(w+)::(w+)$ DEMO

String sets (each line run on a loop in JS):

                 group4::group5
              aa:group4::group5
              bb:group4::group5
              cc:group4::group5
          group1|group4::group5
       group1|aa:group4::group5

          group3:group4::group5
       aa:group3:group4::group5
   group1|group3:group4::group5
group1|aa:group3:group4::group5

Final results should be able to produce this…

var groups = {1:"group1", 2:"group2", 3:"group3", 4:"group4", 5:"group5"};

Regex pattern would be sufficient if anyone can help me on this

Advertisement

Answer

You may use this PCRE regex comprising a branch reset group, 5 separate capture groups, few optional matches and non-capturing groups:

^h*(?:(?:(w+)|)?(?|(?:(aa|bb|cc):(?:(w+):)?)|(?:((?2)):)?(w+):)?)?(w+)::(w+)$

Updated RegEx Demo

PS: branch reset group doesn’t work in Javascript.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement