
I have a string like the following: string myStr = "Monthly Ecosystem NPP = 0.360591 tDM/ha month"; How can I use boost regex to isolate 0.360591, as I would do in bash with a echo $myStr | awk '{ print $5 }' ? Thanks in advance. -- Alessandro Candini MEEO S.r.l. Via Saragat 9 I-44122 Ferrara, Italy Tel: +39 0532 1861501 Fax: +39 0532 1861637 http://www.meeo.it ======================================== "ATTENZIONE:le informazioni contenute in questo messaggio sono da considerarsi confidenziali ed il loro utilizzo è riservato unicamente al destinatario sopra indicato. Chi dovesse ricevere questo messaggio per errore è tenuto ad informare il mittente ed a rimuoverlo definitivamente da ogni supporto elettronico o cartaceo." "WARNING:This message contains confidential and/or proprietary information which may be subject to privilege or immunity and which is intended for use of its addressee only. Should you receive this message in error, you are kindly requested to inform the sender and to definitively remove it from any paper or electronic format."

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Alessandro Candini Sent: Friday, June 24, 2011 1:50 AM To: boost-users@lists.boost.org Subject: [Boost-users] [Regex] Emulate awk
I have a string like the following:
string myStr = "Monthly Ecosystem NPP = 0.360591 tDM/ha month";
How can I use boost regex to isolate 0.360591, as I would do in bash with a echo $myStr | awk '{ print $5 }' ? [snip]
Alessandro,
The first shift to make is that with a RegEx you are dealing with recognizing character types, not delimited substrings as with awk. You could do it that way, by recognizing the 4 spaces and ignoring the stuff in-between.
boost::regex e("^[^\\s]+\\s[^\\s]+\\s[^\\s]+\\s[^\\s]+\\s([^\\s]+)");
... which will create a regex pattern e that starts at the beginning of the string, ignores four batches of non-space stuff followed by spaces, then captures the final non-space batch which is hopefully always your number.
This is easier, just recognizing the batch of numbers with a decimal:
boost::regex exp("([0-9.]+)");
... will create a pattern called exp that will find some string of digits with periods. If you can be sure that there will only be one floating point non-negative number in the string this is all you need. You can also combine the two approaches.
// code
#include
participants (2)
-
Alessandro Candini
-
Wilde, Donald S