Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am new to Python and I do not know regular expressions in Python. Here is the question. I have a sentence like "Total Cost: 37" in stdout, I want to extract the cost info which is 37. When I match the words "Total Cost: " in a specific line how should I get the rest of the line which is the info that I am interested in?

share|improve this question
up vote 3 down vote accepted

use regex:

Total Cost:\s? explanation:

"Total Cost": Match the literal Total Cost:

\s? : 1 to 0 times Whitespace [\t \r\n\f]

Capturing group ([-+]?\d+):

[-+]?: 1 to 0 times matches one of the following chars: -+

\d+ :infinite to 1 times Digit [0-9]

In [121]: strs="some text Total Cost: 37 some more more Total Cost: -100"

In [122]: re.findall(r"Total Cost:\s?([-+]?\d+)",strs)
Out[122]: ['37', '-100']
share|improve this answer

Though you said the question was related to "regex," I focus on what you were saying about the output. You have an STDOUT of:

Total Cost: 37

Assume this output is stored in a string called output_string. I use the Python interpreter:

In [11]: output_string = "Total Cost: 37"

In [13]: (total_text_string, total_numeric_string) = 
         output_string.split(':')

In [14]: total_text_string
Out[14]: 'Total Cost'

In [15]: total_numeric_string
Out[15]: ' 37'

In [16]: float(total_numeric_string)
Out[16]: 37.0

We take the string, use the split method, with ":" as a delimiter. We end up with two strings, one for the textual portion, one containing the numerical portion. Since you likely want the cost to contain decimals, you can "cast" it into a floating point via "float". You can then decide what you want to do with these values.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                       <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\n                </div>\n            </div>\n\n            <div id=\"copyright\">\n                site design / logo &#169; 2016 Stack Exchange Inc; user contributions licensed under <a href=\"https://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\n            </div>\n            <div id=\"svnrev\">\n                rev 2016.2.9.3241\n            </div>\n            \n        </div>\n    </div>\n    <noscript>\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\n    </noscript>\n<scri"], "copyright__copyright__copyright": ["\":\" as a delimiter. We end up with two strings, one for the textual portion, one containing the numerical portion. Since you likely want the cost to contain decimals, you can \"cast\" it into a floating point via \"float\". You can then decide what you want to do with these values.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}