The Governor
The Governor

Reputation: 397

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!

Upvotes: 4

Views: 1626

Answers (4)

The fourth bird
The fourth bird

Reputation: 163577

You could use a capturing group which will be returned by findall:

\(\s*([0-9]+\s*[a-z]+)\s*\)

That will match:

  • \(\s* match ( and 0+ times a whitespace char
  • ( Capturing group
    • [0-9]+\s*[a-z]+ Match 1+ a digit, 0+ times a whitespace char and 1+ times a-z (or use cm instead of [a-z]+ if you want to match that literally)
  • ) Close capturing group
  • \s*\) Match 0+ times a whitespace char

regex101 demo | Python demo

For example:

import re

string = '6\' 3" ( 190 cm )'
pattern = re.compile(r"\(\s*([0-9]+\s*[a-z]+)\s*\)")
print(pattern.findall(string))

Upvotes: 1

Rahil Hastu
Rahil Hastu

Reputation: 558

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

where string is:

'6\' 3" ( 190 cm )'

Upvotes: 3

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39930

With regular expressions:

import re

s = '6\' 3" ( 190 cm )'
desired_output = re.search(r'\((.*?)\)',s).group(1).lstrip()

print(desired_output)
>>> 190 cm

Without regular expressions:

s = '6\' 3" ( 190 cm )'
desired_output = s[s.find("(")+1:s.find(")")].lstrip()

print(desired_output)
>>> 190 cm

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140286

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'

Upvotes: 2

Related Questions