Regular expression to match white space only when space found between only numeric values, and not find the space when comma found between two words

Viewed 49

Input:

Price           45.0          50.0       43.5     43.0, 12.9 NOT FOUND

Expected output:

Price
45.0
50.0
43.5
43.0, 12.9
NOT FOUND

Regular Expression to match white space only when space found between only numeric values:

"(?i)(?:(?<![a-z])\s+|\s+(?![a-z]))"

I don't want to detect space between 43.0, 12.9

2 Answers

You may use

(?<=[A-Za-z\d])\s+(?=\d)|(?<=\d)\s+(?=[A-Za-z])

See the regex demo

Details

  • (?<=[A-Za-z\d])\s+(?=\d) - one or more whitespace chars immediately preceded with an alphanumeric char and immediately followed with a digit
  • | - or
  • (?<=\d)\s+(?=[A-Za-z]) - one or more whitespace chars immediately preceded with a digit char and immediately followed with a letter.
Related