Answer the question
In order to leave comments, you need to log in
Can you help me with a regular expression?
var regul = /(AAA|BBB|CCC|DDD)\(.*?\)/g;
var str = "one two 12AAA(CCC(C1_1);A1_1)dfg "+
"dfgdfgdfg,BBB(B1_1; B1_2) sfsgndf "+
"dfgnhjdfglkjdfg DDD(D1_1)";
var ARR = str.match( regul );
for(var i = 0; i < ARR.length; i++) {
console.log((i+1)+') '+ARR[i]);
}
Answer the question
In order to leave comments, you need to log in
Obviously, we need recursive regular expressions.
https://regex101.com/r/SBqnii/7
You can play around with groups. Probably, I will have to skip the regular expression by the result again, but I think it's easier and maybe even faster than using a state machine. And I would look at an example of such an automaton :)
Performance comparison with a finite automaton:
https://jsperf.com/regex-vs-fsa/1
The answer is not correct, but I wrote it for too long, so I feel sorry for deleting it :D
This is how you can get
1) AAA(CCC(C1_1);A1_1)
;
2) BBB(B1_1; B1_2)
;
3) DDD(D1_1)
.
I'm not sure, but as far as I know, since it CCC(C1_1)
is already in the found (inside AAA(CCC(C1_1);A1_1)
), you won't find it the second time, for this you have to repeat the parsing for group 2 of each inclusion.
Also in the example, you can see that the triple nesting under this rule is no longer suitable. You can extend the regular expression to catch any finite number of nestings, but personally I would not recommend using them for such tasks. I note that I'm not very good at regular seasons, so maybe my advice is incorrect, in which case I apologize.
UPD: Changed the example. The previous regular expression looked for AAA(bar; BBB(); foo) but got confused with AAA(BBB(); foo; DDD(); bar).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question