Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2006-07-27 13:42:39


llwaeva_at_[hidden] wrote:
> Hi there,
> I am using regex_replace for string replacing. I am working with a
> very large string (about 3M) and need to do the following replacment
>
> %R% --> #R#
> %A% --> #R1#
> %C% --> #R2#
>
> and so on.
>
> I am new to regular express and regex. After having read the
> document, I
> know how regex works, but still not sure if regex_replace meet my
> demand
>
> 1) I invoke regex_replace several times to replace difference
> patterns.
> For a large input string, the operation is very slow. Can I call
> regex_replace once to do the several replacments. i.e. calling
> regex_replace( output, begin, end, "%R ,%A ,%C", "#R#, #R1#, #R2#",
> match_default); for replacing
> %R% with #R#
> %A% with #R1#
> %C% with #R2#

You can, use something like:

"(%R%)|(%A%)|(%C%)"

as the regex, and then use the replace string:

"(?1#R#)(?2#R1#)(?3#R2#)"

with the flag "formal_all" set (this enables Boost-specific format-string
extensions that allow you to do conditional search and replace like this).

> 2) According to the document, the output fashion depends on the
> match_flag_type,
> for my case, I hope the output string, including both matched and
> nonmatched part, replace the whole input string. e.g.
>
> SourceStr = "xxxx%R%yyy%A%zzz%C%"
> Apply regex_replace( output_string, SourceStr.begin, SourceStr.end,
> ... )
> We have , output_string = "xxxx#R#yyy#R1#zzz#R2#"
> rather than #R#, #R1# and #R2#

Use "format_no_copy" to suppress copying unmatched parts to the output.
Otherwise the default behaviour is to always copy unmatched parts of the
input to output.

John.


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