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

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

Python Regex: Extract all occurences of a substring within a string

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...