Boost logo

Boost Users :

From: Lynn Allan (l_d_allan_at_[hidden])
Date: 2006-04-12 14:33:47


Eric Niebler wrote:
> Lynn Allan wrote:
>>
>> But, in example 2, seems like "year" should be repeat<1,4>, but:
>> year= repeat<1,2> works:
>>
>> cregex date = (month= repeat<1,2>(_d)) // find the month ...
>>>> (delim= (set= '/','-')) // followed by a delimiter ...
>>>> (day= repeat<1,2>(_d)) >> delim // and a day followed by the same
>>>> delimiter ...
>>>> (year= repeat<1,2>(_d >> _d)); // and the year.
>>
>> actually, repeat<1,3> works for month, day, and year. Am I mixed up
>> on what "repeat" means?
>
>
> repeat<n,m>(X) means to match X between n and m times, inclusive. So
> matching a month a day, you want repeat<1,2>(_d) to match 1 or 2
> digit
> characters, and to match a year, you want repeat<1,2>(_d >> _d) to
> match two digits or four digits. Three digits isn't a common
> representation of a year.

Ok .... and thanks for your patient assistance.

I think I see why repeat<1,2> works for yyyy, but AFAICT, the
repeat<1,3> "worked" for day dd and month mm, which seems off. I
changed the sample code "just to see what would happen" and was
scratch-my-head-surprised to get the same results from repeat<1,2> as
for repeat<1,3> .... days and months were "captured".

But I'm probably doing something wrong or "just don't get it" about
xpressive.

Here is the "tweaked" Example 2 using repeat<1,3>:
void example2()
{
    char const *str = "I was born on 5/30/1973 at 7am.";

    // define some custom mark_tags with names more meaningful than
s1, s2, etc.
    mark_tag day(1), month(2), year(3), delim(4);

    // this regex finds a date
    cregex date = (month= repeat<1,3>(_d)) // find the month
...
>> (delim= (set= '/','-')) // followed by a
delimiter ...
>> (day= repeat<1,3>(_d)) >> delim // and a day
followed by the same delimiter ...
>> (year= repeat<1,3>(_d >> _d)); // and the year.

    cmatch what;

    if( regex_search( str, what, date ) )
    {
        std::cout << "LdaExample2" << '\n'; // whole match
        std::cout << what[0] << '\n'; // whole match
        std::cout << what[day] << '\n'; // the day
        std::cout << what[month] << '\n'; // the month
        std::cout << what[year] << '\n'; // the year
        std::cout << what[delim] << '\n'; // the delimiter
    }
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net