regex - Combining Regexes -
how can combine regexes?
edit: if exam preparation. question write regex find strings have odd number of a's , number of b's?
i.e. instead of | or, need mechanism emulate and
i have 2 regexes:
1) find odd number of a's: ^[^a]*a([^a]*a[^a]*a)*[^a]*$ 2) find number of b's: ^([^b]*b[^b]*b)*[^b]*$
you can using lookahead expressions (here shown verbose regex since hard read, more on single line):
^ # start of string (?=(?:(?:[^a]*a){2})*[^a]*$) # assert number of (?=[^b]*b(?:(?:[^b]*b){2})*[^b]*$) # assert odd number of bs .* # match $ # end of string
the last 2 lines can dropped if you're validating - match entire string.
Comments
Post a Comment