EN VI

Using regex to replace multiple instance of match of hypenated numbers into comma-separated sequences?

2024-03-13 15:30:10
How to Using regex to replace multiple instance of match of hypenated numbers into comma-separated sequences

I'm trying to convert string like this:

1,2,4-10,12,15-19.

I need to convert it to: 1,2,4,5,6,7,8,9,10,12,15,16,17,18,19

Supposed the string is in the cell A1, I got a little success using this formula: =regexreplace(A1;"(\d+\-\d+)+";join(",";sequence(value(regexextract(A1;"\-(\d+)"))-value(regexextract(A1;"(\d+)\-"))+1;1;value(regexextract(A1;"(\d+)\-")))))

this formula yield me result: 1,2,4,5,6,7,8,9,10,12,4,5,6,7,8,9,10

Please help to fix the formula, I'm new to regex.

Solution:

Here's one approach you may test out:

=let(Λ;split(substitute(A3;"-";"`");",");
join(",";tocol(map(Λ;lambda(Σ;if(regexmatch(Σ&"";"`");let(Δ;split(Σ;"`");x;index(Δ;;1);y;index(Δ;;2);sequence(y-x+1;1;x));Σ)));1;1))) 

enter image description here

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login