Types of regex
Simen Kjaeraas
simen.kjaras at gmail.com
Mon Jul 15 02:54:25 PDT 2013
On 2013-07-15, 11:32, Larry wrote:
> Hello,
>
> I read the library reference for regex.
>
> I really miss python's equivalent of finditer.
>
> Sometimes matching is not on purpose and one will want to match all the
> occurences to iterate over it since it is much more regarding concerning
> the orders and repetitions.
>
>
> my code :
> -----------------------------------------------------------------------------
> version(Tango) extern (C) int printf(char *, ...);
>
> import std.stdio;
> import std.regex;
> import std.file;
> import std.format;
>
> int main(char[][] args)
> {
> string fl = readText("testregexd.txt");
>
> auto m = match(fl, regex(`(<n=(?:hello|goodbye))*`,"g"));
> auto c = m.captures;
>
> writeln(c);
>
> return 0;
> }
>
> ---------------------------------------------------------------------------
>
> Content of testregexd.txt:
> ----------------------------------------------------------------------------
>
> <n=hello <n=goodbye
>
> ----------------------------------------------------------------------------
>
> Any way to workaround ?
>
> Thanks !
>
> Larry
Have you tried iterating over m? This works for me:
import std.stdio;
import std.regex;
import std.file;
import std.format;
int main(char[][] args)
{
string fl = "
<n=hello <n=goodbye
";
auto m = match(fl, regex(`(<n=(?:hello|goodbye))`,"g"));
foreach (c; m)
writeln(c);
return 0;
}
--
Simen
More information about the Digitalmars-d-learn
mailing list