boost regex format string

A

Aek

Hi everyone,

I am trying to construct a regular expression and format string to use
with a boost::regex_replace()
In my file the sample text is:

// .fx shader file
FLOAT JOE 3545f;
FLOAT BLOGS -3343f;
FLOAT AMBIENT 2300.0f;


I have the regex to extract the top two incorrect float specifiers
(\s(|-)\d+f;) or the boost equivalent is regex string is
"([[:space:]](|-)[[:digit:]]+f;)"
which returns 3545f; and -3343f;

Now I am a bit confused by how I need to apply the format string to
take these two values and then insert ".0" between the last digit and
the "f;"

What sort of format string would I need for that?
Or am I going about the regex incorrectly to begin with?

Any help is appreciated,
Josh
 
A

Aek

Aek said:
I have the regex to extract the top two incorrect float specifiers
(\s(|-)\d+f;)

I think I have a solution.
I changed the regex to include additional bracket for lexical grouping.
This means that the regex still only returns 2 results for the sample
text I posted earlier, but allows me to deal with each block of text
separately.

regex = (\s(|-)\d+)(f;)
now I can have a format string as
"\\1\\2" to print both the "3000" and "f;"
or
to solve my problem I set the format string to
"\\1.0f;" essentially discarding the \\2 and replacing it with a ".0f;"
 
P

Pete Becker

Aek said:
I think I have a solution.
I changed the regex to include additional bracket for lexical grouping.
This means that the regex still only returns 2 results for the sample
text I posted earlier, but allows me to deal with each block of text
separately.

regex = (\s(|-)\d+)(f;)
now I can have a format string as
"\\1\\2" to print both the "3000" and "f;"
or
to solve my problem I set the format string to
"\\1.0f;" essentially discarding the \\2 and replacing it with a ".0f;"

That looks right, with the minor nit that it's the third submatch that's
being discarded, not the second.

On the other hand, if you're just going to throw away the third
submatch, you don't need to save it. So you can get rid of the
parentheses around f;.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
A

Aek

thanks for your suggestion.
I have removed the parentheses from around the f;

However I seem to be getting an Empty expression exception when
assigning my expression to my boost:regex object.

boost::regex exp;
const char* expression_string = "([[:space:]](|-)[[:digit:]]+)f;";
exp.assign(expression_string);

Adding some additonal parentheses around the whole expression doesnt
seem to change the behaviour either.

Any ideas?
 
A

Aek

Some more info
I am linking against the static libs
Using MS VS.net 2003 and boost 1.33.1
 
A

Aek

I managed to resolve this.For future reference...
Had to change the alternation operator in the middle from

(|-)
to
(?:)|-

as (?:) is used to represent an empty space
 
P

Pete Becker

Aek said:
However I seem to be getting an Empty expression exception when
assigning my expression to my boost:regex object.

boost::regex exp;
const char* expression_string = "([[:space:]](|-)[[:digit:]]+)f;";
exp.assign(expression_string);

I was surprised to see that (|-) seems to be a valid expression
according to the ECMAScript grammar. But my guess is that that's where
the problem is coming from, since the left side of that alternation is
the only empty subexpression. The more idiomatic way to say that is (-?).

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
P

Pete Becker

Aek said:
I managed to resolve this.For future reference...
Had to change the alternation operator in the middle from

(|-)
to
(?:)|-

as (?:) is used to represent an empty space

(-?) is clearer.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top