I have a string like this '6\' 3" ( 190 cm )'
and I would like to extract '190 cm'
only using regular expressions. I can't find the appropriate pattern to look for.
I have tried
string = '6\' 3" ( 190 cm )'
pattern = re.compile(r'[^\\( 0-9+ \\)]')
pattern.findall(a)
but it returns ["'", '"', 'c', 'm']
Thanks for helping!
print re.findall(r'[0-9]+ cm',string)[0]
where string
is:
'6\' 3" ( 190 cm )'
too many unrequired and harmful symbols in your expression.
Using surrounding []
made findall
match individual characters, which explains the output you're getting.
This needs a full rethink: escape the parentheses, use \d+
to match one or more digits, and explicit cm
and spaces.
create a group to match only digits+unit, use search
to find the group and display it.
import re
string = '6\' 3" ( 190 cm )'
pattern = re.compile(r'\( (\d+ cm) \)')
>>> pattern.search(string).group(1)
'190 cm'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With