Re: [pcre-dev] split string using pcre in c

Page principale
Supprimer ce message
Auteur: Thorsten Schöning
Date:  
À: pcre-dev
Sujet: Re: [pcre-dev] split string using pcre in c
Guten Tag Brad Lira,
am Donnerstag, 27. Januar 2011 um 21:44 schrieben Sie:

> use strict;
> use warnings;
> my $str = 'command1 [hello my dear] command2 command3 "command4
> --some-arg arg --some- +other-arg 2" command5 command6';


> for (split m{(\s+|"[^"]*"|\[[^\[]*\])}, $str) {
>             print $_, $/;
>     }


You should be able to use pcrecpp::RE::Consume for that, the only
difference is, that you won't get an array with all tokens at once. I
quote the local doc, because I don't find the link online.

> SCANNING TEXT INCREMENTALLY
>
> The "Consume" operation may be useful if you want to repeatedly
> match regular expressions at the front of a string and skip over
> them as they match. This requires use of the "StringPiece" type,
> which represents a sub-range of a real string. Like RE, StringPiece
> is defined in the pcrecpp namespace.
>   Example: read lines of the form "var = value" from a string.
>      string contents = ...;                 // Fill string somehow
>      pcrecpp::StringPiece input(contents);  // Wrap in a StringPiece

>
>      string var;
>      int value;
>      pcrecpp::RE re("(\\w+) = (\\d+)\n");
>      while (re.Consume(&input, &var, &value)) {
>        ...;
>      }
> Each successful call to "Consume" will set "var/value", and also
> advance "input" so it points past the matched text.

>
>
> The "FindAndConsume" operation is similar to "Consume" but does not
> anchor your match at the beginning of the string. For example, you
> could extract all words from a string by repeatedly calling
> pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)


Mit freundlichen Grüßen,

Thorsten Schöning

--
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig

Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@???
Web:     http://www.am-soft.de


AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow