Rokrait
Rokrait

Reputation: 31

Regex Multiple Capture Groups of Same Pattern inside a match

im new to regex, but i have a question i could not find a answer to. i have a file that looks like this

more text
static abc_p ABC_P[] = {

  /* Netz I */
  {
    N1 
    N1 
    N1 
    0, 
    {
      Flg1
      0,
      Flg2
      0,
      Flg3
      0,
      Flg4
      0,
    },
  },

  /* Netz E1 */
  {
    N2 
    N2 
    N2 
    0, 
    {
      Flg1
      0,
      Flg2
      0,
      Flg3
      0,
      Flg4
      0,
    },
  },
};

more text 

now i want only this pattern to parse it to a class same for N2 and many more text that have the same pattern

    N1 
    N1 
    N1 
    0, 
    {
      Flg1
      0,
      Flg2
      0,
      Flg3
      0,
      Flg4
      0,

now i have this pattern static\s+abc_p\s+ABC_P\[\s*\]\s*=\s*\{(\n((?:[^{]*\{\n([^}]*)\}\,\n\s*\}\,\n)))[^;]* build with regex101 but i does only match the first group then i read somthing about the lazy operator but when i add a + at the end of the las )it matches only the last appearance and with +? it matches only the first appearance is there a way i can modifiy the pattern that i get every appearance of this structure N1 to 0, or should i just delete the first group after i found it and then look for the next group bassicly i found N1 delete it search for next found N2 delete it, search for the next till i reach ;, i want a pattern that gives me N1 into a capture group and same for N2 and NX

Demo

Edit : One solution is to match only the nested arrays is to divide into to seperate matches first to match the array

static\s+abc_p\s+ABC_P\[\]\s*=[^;]+;

and then match the nested arrays and then iterate through the nested array matches

\{(\s*[^}]+)

Upvotes: 1

Views: 67

Answers (0)

Related Questions