fatma.ekici
fatma.ekici

Reputation: 2837

How to match a specific word from the beginning of a sentence and get the unmatched part in Python?

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?

Upvotes: 0

Views: 78

Answers (2)

terryjbates
terryjbates

Reputation: 312

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.

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

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']

Upvotes: 3

Related Questions