c# - How to Combine Arbitrary Regexes with AND -
is there generic way combine regexes together?
while coding solution of 1 of euler problems, code ended performing this:
list<string> expressions; //there 50 regex expr in list list<regex> regexes = new list<regex>(); foreach (string expr in expressions) { regexes.add(new regex(expr, regexoptions.compiled)); } foreach (string line in file.readalllines(...)) { bool matches = true; foreach (regex regex in regexes) { if (!regex.ismatch(line)) { matches = false; break; } } if (matches) { console.writeline("this line matches of regexes: "); console.writeline(line); break; } }
the approach above innefficient since scans every line in file 50 times.
i create 1 regex matches line if 50 regexes match string. way each line scanned once (and unmatching lines fail earlier due more restrictive regex).
(i don't care match, need know if match).
from of cs classes, seem remember learning how hand generating dfas each regex , intersecting them.
so, c# have built-in way combining arbitrary regexes and?
if not, how can achieve same result creating new regex based on anding 2 regexes together? (even better extension method regex).
well, understanding regex in .net. there no solution , 2 regex. however, come :
lookup<tkey, telement> data structure . keys in here collection of lists of regex while elements collection of strings. think search more efficient yours.
you come own state machine. how regex converted state machine
Comments
Post a Comment