1

Pattern matching in Javascript

I want to make pattern which matches the string

{"ABC":["A1","X30","2","A2","X31","3","A3","X90","9"],"XYZ":["A1","X31","2","A3","X40","7"]}
            

I have used regex pattern

{("["A-Z0-9"]+":["[A-Z0-9]+","[A-Z0-9]+","[0-9]+"],)+}

but it doesn't match properly.

Every array length is multiple of 3 only and first & second element in pair of 3 consist of [A-Z0-9] only and third element consist of [0-9] only.

For example: "A1","X30" (first two element) & "2"(third element) similarly after 3 elements this pattern repeats "A2","X31" as first and second element and "3" as third element.

test pattern-1:

{"ABC":["A1","X30","2"],"XYZ":["A1","X31","2","A3","X40","7"]}

test pattern-2:

{"ABC":["A1","X30","2","A2","X31","3","A3","X90","9"],"XYZ":["A1","X31","2","A3","X40","7"]}

test pattern-3:

{"ABC":["A1","X30","2"]}

3 Answers 3

2

You have to optionally repeat the inner part with the 3 parts again:

{"["A-Z0-9"]+":["[A-Z0-9]+","[A-Z0-9]+","[0-9]+"(?:,"[A-Z0-9]+","[A-Z0-9]+","[0-9]+")*](?:,"["A-Z0-9"]+":["[A-Z0-9]+","[A-Z0-9]+","[0-9]+"(?:,"[A-Z0-9]+","[A-Z0-9]+","[0-9]+")*])*}

The pattern in parts:

  • { Match opening {
  • "["A-Z0-9"]+":["[A-Z0-9]+","[A-Z0-9]+","[0-9]+" Match the part before the : followed by 3 parts after it consisting of 2 times [A-Z0-9] and only digits for the 3rd part
  • (?: Non capture group
    • ,"[A-Z0-9]+","[A-Z0-9]+","[0-9]+"
  • )* Close non capture group and optionally repeat
  • ] Match ]
  • (?: Non capture group
    • ,"["A-Z0-9"]+":["[A-Z0-9]+","[A-Z0-9]+","[0-9]+" Match a again the part before the : and the 3 parts after it
    • (?: Non capture group
      • ,"[A-Z0-9]+","[A-Z0-9]+","[0-9]+" Match a comma and repeat the first part again
    • )* Close non capture group and optionally repeat
    • ] Match ]
  • )* Close non capture group and optionally repeat
  • } Match closing }

Regex demo

Sign up to request clarification or add additional context in comments.

3 Comments

{"ABC":["A1","X","2"]} It doesn't work on string like this because there should be a number followed by upper case letter in First and Second Element.
Then you can replace the [A-Z0-9]+ parts with [A-Z][0-9]+ or the other way around.
I have changed [A-Z0-9]+ to [A-Z]+[0-9]+ and it works !! Thanks !!
2

This is a correct pattern:

{("[A-Z0-9]+":\[("[A-Z0-9]+","[A-Z0-9]+","[0-9]+"(,|(?=\])))+\](,|(?=})))+}

where (,|(?=\])) means "match a , or check that it is followed by a ]".

See a demo here.

Anyway, you should use a JSON parser if you want to be sure not to have unwanted behaviors.

2 Comments

{"ABC":["A1","X","2"]} It doesn't work on string like this because there should be a number followed by upper case letter in First and Second Element
1

You can add sub groups.

{("[A-Z0-9]+":\[("[A-Z0-9]+","[A-Z0-9]+","[0-9]+",?)+\],?)+}

https://regex101.com/r/OqjcVv/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.