pythonregexextract

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!


Solution

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