Skip to content
Advertisement

substitute ‘=’ sign when an integer is encountered using python

Hi I am new to python and regex. I have a string which i want to reformat/substitute

JavaScript

i did try with:

JavaScript

Expected output:

JavaScript

Advertisement

Answer

You can match either the start of the string, or a space and comma without using a capture group and assert not a digit after matching a single digit.

JavaScript

The pattern matches

  • (?:^|, ) Non capture group, assert either the start of the string or moatch ,
  • d+(?!/) Match 1+ digits asserting not a / directly to the right

Regex demo | Python demo

In the replacement use the full match followed by an equals sign

JavaScript

Example

JavaScript

Output

JavaScript

Another option could be using a positive lookahead to assert an uppercase char [A-Z] after matching a digit.

JavaScript

Regex demo

Advertisement