How to normilize text with regex with some if statements?
If we have string like this
One T933 two, three35.4. four 9,3 8.5 five M2x13 M4.3x2.1
And I want to normilize like this
one t 933 two three 35.4 four 9,3 8.5 five m2x13 m4.3x2.1
- Remove all dots and commas.
- Split number and string if not starts with letter 'M'
T933-->T 933 - All lowercase
- Do not split if there is dot or comma between numbers
35.4-->35.4or9,3-->9.3if there is comma between, then replace to dot
What I am able to do is this
def process(str, **kwargs):
str = str.replace(',', '.')
str = re.split(r'(-?\d*\.?\d+)', str)
str = ' '.join(str)
str.lower()
return str
but there is no if condition when numbers starts with letter 'M' and their also is splitted. And in some reason after string process i get some unnecessary spaces.
Is there some ideas how to do that with regex? Or with help methods like replace, lower, join and so on?
I can suggest a solution like
The outer
re.subis meant to remove dots or commas when not between digits:[.,]- a comma or dot(?!(?<=\d.)\d)- a negative lookahead that fails the match if there is a digit immediately to the right, that is immediately preceded with a digit + any one charThe inner
re.subreplaces with a space the following pattern:(?<=[^\W\d_])(?<![MmXx])(?=\d)- a location between a letter ([^\W\d_]matches any letter) and a digit (see(?=\d)), where the letter is notMorX(case insensitive,[MmXx]can be written as(?i:[mx]))|- or(?<=\d)(?=[^\W\d_])- a location between a digit and a letter.See the Python demo:
Output: