473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

boost regex format string

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

Oct 26 '06 #1
7 4799
Aek

Aek wrote:
>
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;"

Oct 26 '06 #2
Aek wrote:
Aek wrote:
>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;"
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.
Oct 26 '06 #3
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?
Pete Becker wrote:
Aek wrote:
Aek wrote:
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;"

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.
Oct 27 '06 #4
Aek
Some more info
I am linking against the static libs
Using MS VS.net 2003 and boost 1.33.1

Oct 27 '06 #5
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

Oct 27 '06 #6
Aek wrote:
>
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.
Oct 27 '06 #7
Aek wrote:
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.
Oct 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Richard Latter | last post by:
Hello All, I am a newbie to the Boost library and I have a question about a simple function. All I would like to do is to create a simple function that can test strings using regular...
5
by: FBergemann | last post by:
I use SunOS 5.8, gcc 3.3.2, boost 1.33.1. I have build the entire boost package and try to compile a simple example: #include <iostream> #include <string> #include <boost/regex.hpp //...
3
by: MCH | last post by:
hi there, I am working with a HTML-like text with boost:regex. For example, the following pattern might occur in my text <abc efg> <p>EFG</p 12<3> In this case, I would like to extract...
1
by: Alan Patterson | last post by:
I have installed boost 1.33.1 and everything compiled and installed fine on Windows using Visual C++ 8.0. I wrote the following simple code to test the regex library and it doesn't seem to work....
1
by: Barry | last post by:
I used Boost.Xpressive after 1.3.4, and found it interesting and helpful, but I suffer from long complie-time. So I wonder there is a way to write the regular expression in Xpressive and use it in...
3
by: neino | last post by:
Hello, did anyone of You have problems using boost::regex ? That code below : #include "boost/regex.hpp" /*1*/ int main() /*2*/ { /*3*/...
13
by: brad | last post by:
Still learning C++. I'm writing some regex using boost. It works great. Only thing is... this code seems slow to me compared to equivelent Perl and Python. I'm sure I'm doing something incorrect....
2
by: skip | last post by:
A colleague wrote a C++ library here at work which uses the Boost.regex library. I quickly discovered an apparent problem with how it searches. Unlike re.match the regex_match function in that...
3
by: rottmanj | last post by:
In my application I get a string from my datasource that I need to replace some of the bad chars in the strings. Right now I have written a pretty standard regex that should replace any of the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.