Python Regular expressions remove commas in numbers (Python regex matches comma)

Source: Internet
Author: User
Analysis

The number is often a group of 3 numbers followed by a comma, so the rule is: ***,***,***

Regular type

The code is as follows:


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

The code is as follows:


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

Using function Finditer (string[, pos[, Endpos]) | Re.finditer (pattern, string[, flags]):

Searches for a string that returns an iterator that accesses each matching result (match object) sequentially.

The code is as follows:


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 for the specific problem, that is, the number 3 bit a group, if the numbers mixed with the letter, kill the comma between the numbers, that is, "ABC,123,4,789,MNP" into "ABC,1234789,MNP"

More specifically, find the regular "numbers, numbers," and then remove the comma after the replacement

The code is as follows:


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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.