std.regex with multiple matches

David Gileadi gileadis at NSPMgmail.com
Thu Apr 21 11:42:10 PDT 2011


On 4/21/11 11:36 AM, Dmitry Olshansky wrote:
> On 21.04.2011 21:43, David Gileadi wrote:
>> I was using std.regex yesterday, matching a regular expression against
>> a string with the "g" flag to find multiple matches. As the example
>> from the docs shows (BTW I think the example may be wrong; I think it
>> needs the "g" flag added to the regex call), you can do a foreach loop
>> on the matches like:
>>
>> foreach(m; match("abcabcabab", regex("ab")))
>> {
>> writefln("%s[%s]%s", m.pre, m.hit, m.post);
>> }
>>
>> Each match "m" is a RegexMatch, which includes .pre, .hit, and .post
>> properties to return ranges of everything before, inside, and after
>> the match.
>>
>> However what I really wanted was a way to get the range between
>> matches, i.e. since I had multiple matches I wanted something like
>> m.upToNextMatch.
> I might be wrong but I think that you are looking for std.regex.splitter:
>
> auto s1 =", abc, de, fg, hi,";
> assert(equal(splitter(s1, regex(", *")),
> ["","abc","de","fg","hi",""][]))
>
> Simply put it gets you range of slices of input separated by regex matches.

I considered that but I also need the content of the matches--the 
captures, etc.


More information about the Digitalmars-d-learn mailing list