Home > Blockchain >  Regular Expression in Python by Matches
Regular Expression in Python by Matches

Time:11-02

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

CodePudding user response:

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
  • Related