Very Stupid Regex question
Justin Whear via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 7 09:12:59 PDT 2014
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");
}
More information about the Digitalmars-d-learn
mailing list