4

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!

3
  • 1
    Try (?<=\()[^)]+(?=\)) or use a capturing group \(\s*([0-9]+\s*[a-z]+)\s*\) Commented Mar 2, 2019 at 11:20
  • 1
    [0-9]+ or \d+ Commented Mar 2, 2019 at 11:20
  • 1
    @Thefourthbird Thank you very much,it worked like a charm! Commented Mar 2, 2019 at 11:28

4 Answers 4

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

where string is:

'6\' 3" ( 190 cm )'
Sign up to request clarification or add additional context in comments.

Comments

2

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'

Comments

2

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

Comments

1

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.