Search code examples
regexconditional-statementsquantifiers

regex adding a special character if the quantifier condition is respected


Input

R34, I9012, A39, A, A3939, X9393, B9393939393, B39, A3

Desired Output

R34, I90.12, A39, A, A39.39, X93.93, B93.93939393, B39, A3

Explanation

I would like to add a dot after 3 characters only if the element of the list contains 4 character or more (so for instance I want to get "I90.12" but I dont want to get "R34." ) .

Partial regex to the solution

I can detect the elements that needs to be modified with the following:

([A-Za-z])\w{3,}

but I still can't get the final output I wish to get.


Solution

  • Try using the following regex :

    ([a-zA-Z0-9]{3})(\w+)
    

    and replace the match with the captured groups $1.$2

    DEMO