Very Stupid Regex question

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 7 09:11:26 PDT 2014


On Thursday, 7 August 2014 at 16:05:17 UTC, seany wrote:
> Cosider please the following:
>
> string s1 = PREabcdPOST;
> string s2 = PREabPOST;
>
>
> string[] srar = ["ab", "abcd"];
> // this can not be constructed with a particular order
>
> foreach(sr; srar)
> {
>
>   auto r = regex(sr; "g");
>   auto m = matchFirst(s1, r);
>   break;
>   // this one matches ab
>   // but I want this to match abcd
>   // and for s2 I want to match ab
>
> }
>
> 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?

It's not clear to me what exactly you want, but:

Are the regexes in `srar` related? That is, does one regex always 
include the previous one as a prefix? Then you can use optional 
matches:

     /ab(cd)?/

This will match "abcd" if it is there, but will also match "ab" 
otherwise.


More information about the Digitalmars-d-learn mailing list