regex_replace()

Is there a way to get to know the number of replacements done by
regex_replace(), or at least, whether there was a replacement at all?
Ok, I can compare if(output != input) after regex_replace(), but this is
wasted performance, if there is a better way.

If a regex_error is thrown, how can I get the position of the error in the
regular expression string?

I found that icase (ignore case) only works with A-Za-z, but e.g. not with
German Umlaute (Ää etc.), though I set my German user locale using
regex::imbue(). Am I missing something?

Advertisements

On 10 mai, 09:23, Friedel Jantzen <> wrote:
> Hi!
> Using MSVS 2008, STL TR1 <regex>.
>
> Is there a way to get to know the number of replacements done by
> regex_replace(), or at least, whether there was a replacement at all?
> Ok, I can compare if(output != input) after regex_replace(), but this is
> wasted performance, if there is a better way.

You can roll your own: regex_replace basically instantiate a
regex_iterator from the parameters an performs the replace. It
shouldn't be too hard.
> If a regex_error is thrown, how can I get the position of the error in the
> regular expression string?

AFAIS you cannot; and POSIX regcomp doesn't give more information
either.
You will need a regex format validator.
> I found that icase (ignore case) only works with A-Za-z, but e.g. not with
> German Umlaute (Ää etc.), though I set my German user locale using
> regex::imbue(). Am I missing something?

On 10 mai, 16:47, Juha Nieminen <> wrote:
> Michael Doubez <> wrote:
> > You will need a regex format validator.
>
> You can use a regexp to validate a regexp string. There would be a
> marvelous conceptual recursion there...

I hesitated to make the joke but IMO regex grammar is not powerful
enough to validate regex expression.

I actually tried to find one available on the net in C or C++ but,
strangely, none seem readily available (I didn't look too hard, just
googled a bit).

Thank you for your reply!
....
> You can roll your own: regex_replace basically instantiate a
> regex_iterator from the parameters an performs the replace. It
> shouldn't be too hard.

I wrote test code to do this, but as STL regex is new for me, I thought I
could have missed something and reinvent the wheel.
....
> AFAIS you cannot; and POSIX regcomp doesn't give more information
> either.
> You will need a regex format validator.

>> I found that icase (ignore case) only works with A-Za-z, but e.g. not with
>> German Umlaute (Ää etc.), though I set my German user locale using
>> regex::imbue(). Am I missing something?
>
> This may not be implemented or handled correctly by the compiler.

On 11 mai, 08:02, Friedel Jantzen <> wrote:
> >> I found that icase (ignore case) only works with A-Za-z, but e.g. not with
> >> German Umlaute (Ää etc.), though I set my German user locale using
> >> regex::imbue(). Am I missing something?
>
> > This may not be implemented or handled correctly by the compiler.
>
> Yes, it looks somehow "premature" to me.

You could try toupper/tolower with your local and see if works on the
umlaut (and the eszett ).

Juha Nieminen <> writes:
> Michael Doubez <> wrote:
>> You will need a regex format validator.
>
> You can use a regexp to validate a regexp string. There would be a
> marvelous conceptual recursion there...

Nope, at least not by itself. The language of regexps not itself regular.
I don't know the exact details of TR1 regexps, but I doubt they can
check for matched parentheses.

On Tue, 2011-05-10, Michael Doubez wrote:
> On 10 mai, 09:23, Friedel Jantzen <> wrote:
....
>> If a regex_error is thrown, how can I get the position of the error in the
>> regular expression string?
>
> AFAIS you cannot; and POSIX regcomp doesn't give more information
> either.
> You will need a regex format validator.

POSIX gives you *something* using regerror(3); I assume it's more than
"your regexp is broken" but less than "the problem is the backslash in
position 42".

Regexps are best used hard-coded anyway, rather than generated on the
fly or (worse) generated from user input. So this is usually not a
major problem.

This one's tricky. It's why Unicode introduced title case: if
you ever really wanted to do this, what you'd what to get would
be: "SChoenes Oesterreich". Not sure what that might mean in
the context of regular expressions, however; you'd probably want
a flag stating whether substitution should use a) the case of
the original, b) title case if the original was upper case, or
c) context sensitive title case.
> I wonder if it works on e.g. a French system with sth. like é and É ?

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!