On Thu, Aug 07, 2014 at 04:49:05PM +0000, seany via Digitalmars-d-learn wrote: > On Thursday, 7 August 2014 at 16:12:59 UTC, Justin Whear wrote: > >On Thu, 07 Aug 2014 16:05:16 +0000, seany wrote: > > > >>obviously there are ways like counting the match length, and then > >>using the maximum length, instead of breaking as soon as a match is > >>found. > >> > >>Are there any other better ways? > > > >You're not really using regexes properly. You want to greedily match > >as much as possible in this case, e.g.: > > > >void main() > >{ > > import std.regex; > > auto re = regex("ab(cd)?"); > > assert("PREabcdPOST".matchFirst(re).hit == "abcd"); > > assert("PREabPOST".matchFirst(re).hit == "ab"); > > > >} > > thing is, abcd is read from a file, and in the compile time, i dont > know if cd may at all be there or not, ir if it should be ab(ef)
So basically you have a file containing regex patterns, and you want to find the longest match among them? One way to do this is to combine them at runtime: string[] patterns = ... /* read from file, etc. */; // Longer patterns match first patterns.sort!((a,b) => a.length > b.length); // Build regex string regexStr = "%((%(%c%))%||%)".format(patterns); auto re = regex(regexStr); ... // Run matches against input char[] input = ...; auto m = input.match(re); auto matchedString = m.captures[0]; T -- When solving a problem, take care that you do not become part of the problem.