The python regular expression removes the comma of python from the number and matches the comma

  • 2020-04-02 13:16:50
  • OfStack

Analysis of the

Numbers are often three Numbers in a group, followed by a comma, so the rule is: ***, ***, ***

Regular type


[a-z]+,[a-z]?


import re
sen = "abc,123,456,789,mnp"
p = re.compile("d+,d+?")
for com in p.finditer(sen):
    mm = com.group()
    print "hi:", mm
    print "sen_before:", sen
    sen = sen.replace(mm, mm.replace(",", ""))
    print "sen_back:", sen, 'n'

skills

Use function finditer(string[, pos[, endpos]]) | re. Finditer (pattern, string[, flags]):

Searches for a string, returning an iterator that accesses each Match result (Match object) in sequence.


sen = "abc,123,456,789,mnp"
while 1:
    mm = re.search("d,d", sen)
    if mm:
        mm = mm.group()
        sen = sen.replace(mm, mm.replace(",", ""))
        print sen
    else:
        break

Such a program is specific to the problem, that is, the Numbers in a group of three, if the Numbers mixed with letters, the number between the comma, that is, "ABC,123,4,789, MNP" into "ABC,1234789, MNP"

More specifically, find the regular "number, number" and replace it with a comma


sen = "abc,123,4,789,mnp"
while 1:
    mm = re.search("d,d", sen)
    if mm:
        mm = mm.group()
        sen = sen.replace(mm, mm.replace(",", ""))
        print sen
    else:
        break
print sen


Related articles: