c# - Multiplication with .NET regular expressions -
in spirit of polygenelubricants' efforts silly things regular expressions, try .net regex engine multiplicate me.
this has, of course, no practical value , meant purely theoretical exercise.
so far, i've arrived @ monster, should check if number of 1s multiplied number of 2s equals number of 3s in string.
regex regex = new regex( @" ^ (1(?<a>))* # increment each 1 (2(?<b>))* # increment b each 2 (?(a) # if > 0 ( (?<-a>) # decrement (3(?<c-b>))* # match 3's, decrementing b , incrementing c until # there no 3's left or b 0 (?(b)(?!)) # if b != 0, fail (?<b-c>)* # b = c, c = 0 ) )* # repeat (?(a)(?!)) # if != 0, fail (?(c)(?!)) # if c != 0, fail $ ", regexoptions.ignorepatternwhitespace);
unfortunately, not working, , @ loss why. commented show think engine should doing, may off here. examples of output:
regex.ismatch("123") // true, correct regex.ismatch("22") // true, correct regex.ismatch("12233") // false, incorrect regex.ismatch("11233"); // true, correct
any thought welcome!
i'm pretty sure problem in line:
(?<b-c>)*
from can tell, no text match in there, regex refuses match more 1 time. slimmed down regex following:
(1(?<a>))* (?(a)(?<-a>))* (?(a)(?!))
which passes on 1
fails on 111
. tried (?<-a>)*
. no difference. however, changing to
(1(?<a>))* (?(a)((?<-a>)(2(?<b>))(?<-b>)))* (?(a)(?!))
passes on both 12
, 111222
. going match of ""
match causes regex work expected.
getting original regex, guess (?<b-c>)*
matching 0-1 times, explains why having 1 2 in string works, having more 1 fails.
using string of 11
fails, follows same logic, makes entire match ""
, means matches once, causing (?(a)(?!))
fail.
Comments
Post a Comment