Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to extract all occurrences of a substring within a string using Python Regex. This is what I have tried:

import re
line = "The dimensions of the first rectangle: 10'x20', second rectangle: 10x35cm, third rectangle: 30x35cm"
m = re.findall(r'd+x.*?[a-zA-Z]', line)
print (m)

The output I am getting is ['10x35c', '30x35c']

The output I am trying to achieve is ['10'x20'', '10x35cm', '30x35cm']


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

You may use this regex:

r"d+['"]?xd+['"]?(?:s*[a-zA-Z]+)?"

RegEx Demo

Code:

>>> import re
>>> line = "The dimensions of the first rectangle: 10'x20', second rectangle: 10x35cm, third rectangle: 30x35cm"
>>> print (re.findall(r"d+['"]?xd+['"]?(?:s*[a-zA-Z]+)?", line))
["10'x20'", '10x35cm', '30x35cm']

RegEx Details:

  • d+: Match 1+ digits
  • ['"]?: Match optional ' or "
  • x: Match letter x
  • d+: Match 1+ digits
  • ['"]?: Match optional ' or "
  • (?:s*[a-zA-Z]+)?: Match optional units comprising 1+ letters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...