Regular Expression in Python by Matches

Question:

I have a list of numbers that I am trying to pull out of text. Here is what I am working with:

Expression = r"(d+.)?(d+.)?(d+.)?(d+.)?(*|d+)"
ExpressionResult = (re.findall(Expression, str(OriginalString)))
print('original')
print(str(OriginalString))
print('parsed')
print(str(ExpressionResult))

Here is my output:

original
['', '', '7', '', '', '', '9.2', '', '', '', '9.1', '', '', '', '9.2', '', '', '', '9.5.5', '', '', '', '9.5', '', '', '', '10.1.1', '', '', '', '10.1', '', '', '']
parsed
[('', '', '', '', '7'), ('9.', '', '', '', '2'), ('9.', '', '', '', '1'), ('9.', '', '', '', '2'), ('9.', '5.', '', '', '5'), ('9.', '', '', '', '5'), ('10.', '1.', '', '', '1'), ('10.', '', '', '', '1')]

It looks like the expression is being split up by groups rather than matches. How do I get the match?

Expected output:

7, 9.2, 9.1, 9.2, 9.5.5, 9.5, 10.1.1, 10.1
Asked By: Acuity

||

Answers:

Not exactly sure why you are using regex?
Just use list comprehension to create new list, and if needed join the list using the .join() function:

OriginalString = ['', '', '7', '', '', '', '9.2', '', '', '', '9.1', '', '', '', '9.2', '', '', '', '9.5.5', '', '', '', '9.5', '', '', '', '10.1.1', '', '', '', '10.1', '', '', '']

ExpressionResult = [x for x in OriginalString if x != '']
print(", ".join(ExpressionResult))

OUTPUT:

7, 9.2, 9.1, 9.2, 9.5.5, 9.5, 10.1.1, 10.1
Answered By: ScottC
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.