Regex Submatch Position Question

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Regex Submatch Position Question

admin-100
Hi all,

Quick question about the Boost::Regex library.  Is there a way to
identify, with sub-matches, where a match in the search string is made and
the length of the match?  For instance, with the following regular
expression and search string:

Regex ==> "(\d+)"
Search String ==> "Give me 100 dollars or give me 2001 dollars."

Is there a way to determine that the subexpression matched at positions 8
and 31 in the search string, and that the lengths of the matches were
three and four characters?

Thanks

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Regex Submatch Position Question

l_d_allan
If I understand your question, the snippet sample code related to "Captures"
can be adapted:
http://www.boost.org/libs/regex/doc/captures.html

HTH

----- Original Message -----
From: <[hidden email]>
Newsgroups: gmane.comp.lib.boost.user
Sent: Friday, April 07, 2006 12:19 PM
Subject: Regex Submatch Position Question


> Hi all,
>
> Quick question about the Boost::Regex library.  Is there a way to
> identify, with sub-matches, where a match in the search string is made and
> the length of the match?  For instance, with the following regular
> expression and search string:
>
> Regex ==> "(\d+)"
> Search String ==> "Give me 100 dollars or give me 2001 dollars."
>
> Is there a way to determine that the subexpression matched at positions 8
> and 31 in the search string, and that the lengths of the matches were
> three and four characters?
>
> Thanks


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Regex Submatch Position Question

admin-100
I'm taking a look at http://www.boost.org/libs/regex/doc/captures.html but
I'm not seeing how to access the capture start location with match_extra?
I'll keep digging but if anyone out there knows of a shortcut to the answer,
it'd be appreciated.

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Lynn Allan
Sent: Friday, April 07, 2006 6:04 PM
To: [hidden email]
Subject: Re: [Boost-users] Regex Submatch Position Question

If I understand your question, the snippet sample code related to "Captures"

can be adapted:
http://www.boost.org/libs/regex/doc/captures.html

HTH

----- Original Message -----
From: <[hidden email]>
Newsgroups: gmane.comp.lib.boost.user
Sent: Friday, April 07, 2006 12:19 PM
Subject: Regex Submatch Position Question


> Hi all,
>
> Quick question about the Boost::Regex library.  Is there a way to
> identify, with sub-matches, where a match in the search string is made and
> the length of the match?  For instance, with the following regular
> expression and search string:
>
> Regex ==> "(\d+)"
> Search String ==> "Give me 100 dollars or give me 2001 dollars."
>
> Is there a way to determine that the subexpression matched at positions 8
> and 31 in the search string, and that the lengths of the matches were
> three and four characters?
>
> Thanks


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Regex Submatch Position Question

John Maddock
In reply to this post by admin-100
> Quick question about the Boost::Regex library.  Is there a way to
> identify, with sub-matches, where a match in the search string is
> made and the length of the match?  For instance, with the following
> regular expression and search string:
>
> Regex ==> "(\d+)"
> Search String ==> "Give me 100 dollars or give me 2001 dollars."
>
> Is there a way to determine that the subexpression matched at
> positions 8 and 31 in the search string, and that the lengths of the
> matches were three and four characters?

Take a look at the position(sub) and length(sub) member functions of
match_results<>

sub_match also has a length() member function, but no position() because it
doesn't know where the search started from (unlike match_results which
does).

There's no need to get involved with repeated captures and match_extra etc
BTW.

John.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Regex Submatch Position Question

admin-100
<snip>
>> Regex ==> "(\d+)"
>> Search String ==> "Give me 100 dollars or give me 2001 dollars."
>>
>> Is there a way to determine that the subexpression matched at
>> positions 8 and 31 in the search string, and that the lengths of the
>> matches were three and four characters?
</snip>

>Take a look at the position(sub) and length(sub) member functions of
>match_results<>
>
>sub_match also has a length() member function, but no position() because it
>doesn't know where the search started from (unlike match_results which
>does).
>
>There's no need to get involved with repeated captures and match_extra etc
>BTW.
>
>John.

Thank you, I think that's exactly what I need.  Appreciate it!

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Regex Submatch Position Question

l_d_allan
In reply to this post by admin-100
Michael Coles, MCDBA wrote:
> I'm taking a look at
> http://www.boost.org/libs/regex/doc/captures.html but I'm not seeing
> how to access the capture start location with match_extra? I'll keep
> digging but if anyone out there knows of a shortcut to the answer,
> it'd be appreciated.

<alert comment="boost newbie">

Here's some code adapted from the regex chapter in:
"Beyond the C++ Standard Library ... an Introduction to Boost:

#include <iostream>
#include <boost/regex.hpp>
class regex_callback {
private:
  int sum_;
public:
  regex_callback() : sum_(0) {}

  template <typename T> void operator() (const T& what) {
    sum_ += atoi(what[1].str().c_str());
    std::cout << "Position: " << what.position(1)
            << " Length: " << what.length(1)
           << std::endl;
  }

   int sum() const
  {
      return sum_;
  }
};

int main(int argc, char** argv)
{
   boost::regex reg("(\\d+),?");
   std::string s="1,1,2,3,5,8,13,21";
   boost::sregex_iterator it(s.begin(), s.end(), reg);
   boost::sregex_iterator end;
   regex_callback c;
   int sum = for_each(it, end, c).sum();
   std::cout << "Sum: " << sum << std::endl;
   return 0;
}

</alert>

>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Lynn Allan
> Sent: Friday, April 07, 2006 6:04 PM
> To: [hidden email]
> Subject: Re: [Boost-users] Regex Submatch Position Question
>
> If I understand your question, the snippet sample code related to
> "Captures"
>
> can be adapted:
> http://www.boost.org/libs/regex/doc/captures.html
>
> HTH
>
> ----- Original Message -----
> From: <[hidden email]>
> Newsgroups: gmane.comp.lib.boost.user
> Sent: Friday, April 07, 2006 12:19 PM
> Subject: Regex Submatch Position Question
>
>
>> Hi all,
>>
>> Quick question about the Boost::Regex library.  Is there a way to
>> identify, with sub-matches, where a match in the search string is
>> made and the length of the match?  For instance, with the following
>> regular expression and search string:
>>
>> Regex ==> "(\d+)"
>> Search String ==> "Give me 100 dollars or give me 2001 dollars."
>>
>> Is there a way to determine that the subexpression matched at
>> positions 8 and 31 in the search string, and that the lengths of
>> the
>> matches were three and four characters?
>>
>> Thanks
>
>
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users 


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users