std.regex with multiple matches

David Gileadi gileadis at NSPMgmail.com
Thu Apr 21 13:37:39 PDT 2011


On 4/21/11 1:29 PM, Kai Meyer wrote:
> On 04/21/2011 11:43 AM, 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.
>>
>> Since I'm not very familiar with ranges, am I missing some obvious way
>> of doing this with the existing .pre, .hit and .post properties?
>>
>> -Dave
>
> There's two ways I can think of off the top of my head.
>
> I don't think D supports "look ahead", but if it did you could match
> something, then capture the portion afterwards (in m.captures[1]) that
> matches everything up until the look ahead (which is what you matched in
> the first place).
>
> Otherwise, you could manually capture the ranges like this (captures the
> first word character after each word boundry, then prints the remaining
> portion of the word until the next word boundary followed by a word
> character):

(snip an excellent explanation)

Ahh yes, that's a good way of doing it--track the lengths and slice the 
original array to get the "betweens".  Thanks for the insight!


More information about the Digitalmars-d-learn mailing list