Ritika
Ritika

Reputation: 79

Find occurrences where a comma is separated by numbers using regex

I need to find occurrences where a comma is separated by a number on either side.

For example:

SampleString ='A,B,C, 1, 2, 3, 4, G'

I need to replace the commas with all space before and after comma with just a comma(no space). I need to do this only if the comma is separating numbers.

CleanString =re.sub(r([0-9]+)\s?,\s?([0-9]+)' ,r"\g<1>,\g<2>",SampleString)

This output gives me A,B,C, 1,2, 3,4, G. The output I need is A,B,C, 1,2,3,4, G

Upvotes: 0

Views: 151

Answers (3)

Djaouad
Djaouad

Reputation: 22776

You can use the following regex:

import re

SampleString = 'A, B,B, M1, 3, 44, 45, 46, 47, G, 1, 33'

CleanString = re.sub(r'(\b\d+)(\s*,\s*)(?=\d+\b)', r'\1,', SampleString)

print(CleanString)

Output:

A, B,B, M1, 3,44,45,46,47, G, 1,33

Upvotes: 1

Walter Tross
Walter Tross

Reputation: 12624

CleanString = re.sub(r"(?<=\d)\s*,\s*(?=\d)", ",", SampleString)

(?<=\d) is a lookbehind that requires one digit (\d) to precede the current position

(?=\d) is a lookahead that requires one digit to follow the current position

You don't need to capture anything, i.e., you don't any extra parentheses. Anyway it may be good to know that lookaheads and lookbehinds are non-capturing (unless they contain parentheses inside).

Upvotes: 0

Booboo
Booboo

Reputation: 44108

This regex uses lookahead:

((\d+)\s*,\s*)(?=\d)

This matches digits optionally followed by spaces followed by a comma optionally followed by spaces only if that sequence is followed by a digit. So, in the string 1, 2 , 3, the first match is 1,.

import re
SampleString ='A,B,C, 1, 2 , 3 , 4, G'
CleanString = re.sub(r'((\d+)\s*,\s*)(?=\d)', r"\g<2>,", SampleString)
print(CleanString)

Prints:

A,B,C, 1,2,3,4, G

Upvotes: 0

Related Questions