Very Stupid Regex question

seany via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 7 09:49:05 PDT 2014


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)


More information about the Digitalmars-d-learn mailing list