Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python capture a specific pattern inside a string with regular expressions

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!

like image 279
The Governor Avatar asked Jan 27 '23 15:01

The Governor


2 Answers

print re.findall(r'[0-9]+ cm',string)[0]

where string is:

'6\' 3" ( 190 cm )'
like image 108
Rahil Hastu Avatar answered Jan 29 '23 05:01

Rahil Hastu


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'
like image 24
Jean-François Fabre Avatar answered Jan 29 '23 04:01

Jean-François Fabre