Hướng dẫn python regex remove commas

Use a zero-width negative lookahead to make sure the to be replaced substrings [commas here] are not followed by {space[s]}{digit} at the end:

,[?!\s+\d$]

Example:

In [227]: text = '52A, XYZ Street, ABC District, 2'

In [228]: re.sub[',[?!\s+\d$]', '', text]
Out[228]: '52A XYZ Street ABC District, 2'

Edit:

If you have more commas after the ,{space[s]}{digit} substring, and want to keep them all, leverage a negative lookbehind to make sure the commas are not preceded by {space}{digit[A-Z]}:

[?

Example:

In [229]: text = '52A, XYZ Street, ABC District, 2, M, Brown'

In [230]: re.sub['[?

Chủ Đề