regex - Optimize this horrible regular expression -
regex - Optimize this horrible regular expression -
the thought allow 4 instances of 'a' , 2 instances of 'b' in string of arbitrary length.
now, other characters don't matter, care about, 4 'a's , 2 'b's. came with, this:
m{ ^[^ab]* ( (b[^ab]*b[^ab]*a[^ab]*a[^ab]*a[^ab]*a)| (b[^ab]*a[^ab]*b[^ab]*a[^ab]*a[^ab]*a)| (b[^ab]*a[^ab]*a[^ab]*b[^ab]*a[^ab]*a)| (b[^ab]*a[^ab]*a[^ab]*a[^ab]*b[^ab]*a)| (b[^ab]*a[^ab]*a[^ab]*a[^ab]*a[^ab]*b)| (a[^ab]*b[^ab]*b[^ab]*a[^ab]*a[^ab]*a)| (a[^ab]*b[^ab]*a[^ab]*b[^ab]*a[^ab]*a)| (a[^ab]*b[^ab]*a[^ab]*a[^ab]*b[^ab]*a)| (a[^ab]*b[^ab]*a[^ab]*a[^ab]*a[^ab]*b)| (a[^ab]*a[^ab]*b[^ab]*b[^ab]*a[^ab]*a)| (a[^ab]*a[^ab]*b[^ab]*a[^ab]*b[^ab]*a)| (a[^ab]*a[^ab]*b[^ab]*a[^ab]*a[^ab]*b)| (a[^ab]*a[^ab]*a[^ab]*b[^ab]*b[^ab]*a)| (a[^ab]*a[^ab]*a[^ab]*b[^ab]*a[^ab]*b)| (a[^ab]*a[^ab]*a[^ab]*a[^ab]*b[^ab]*b) ) [^ab]*$ }x;
(as always, perl regex)
is there improve solution this, other simply regrouping cases additional parenthesis? permuting accepted cases seems bit "wrong" in regex terms me.
this question asked on stackoverflow under different pretext. question removed then, since poorly written, no effort @ solving made, , more clear, assignment tcs course. tried answering question anyway , presented message, question has been deleted (in mean time). problem @ hand seems quite interesting me anyway, decided inquire again, improve pretext.)
four a
requirement:
[^a]*(?:a[^a]*){4}
two b
requirment:
[^b]*(?:b[^b]*){2}
combined encapsulating first requirement in lookahead:
^(?=[^a]*(?:a[^a]*){4}$)[^b]*(?:b[^b]*){2}$
regex optimization
Comments
Post a Comment