Regular expressions

I have this text:

“Saldo: 305.78 CUP. Datos: 30.99 GB + 5.24 GB LTE. Voz: 00:42:08.
SMS: 212. Linea activa hasta 31-01-26 vence 02-03-26.”

Stored in a global variable, I want to extract each number separately (including date) to show it in a widget so that it looks something like this (Not exactly):

-Saldo: 305.78 CUP
-Datos: 30.99 GB
-LTE: 5.24 GB
-Voz: 42:08 min (if value were ie: 01:20:10 then:
Voz: 01:20:10 H)
-SMS: 212
-Activa: 31/01/26
-Vence: 02/03/26

How can I use regular expressions to solve it?

Hi there,

Based on your reference, here’s the code snippet to parse the data using regular expression.

Assuming your global variable is: theData

$
"- "+
tc(reg,
  tc(reg,
    tc(reg,
      gv(theData),
      "((V|v)oz\:\s*?)([0-9:]+)", "$1$3" + if(
        tc(count,
          tc(reg,
            gv(theData),
            "((V|v)oz\:\s*?)([0-9:]+)", "$3"
          ), ":"
        )>1,
        " H",
        tc(count,
          tc(reg,
            gv(theData),
            "((V|v)oz\:\s*?)([0-9:]+)", "$3"
          ), ":"
        )>0,
        " min",
        ""
      )
    ),
    "(\.\s+?)([A-Za-z])", "= $2"
  ),
  "=", "
-"
)
$

Screenshot

I hope this would help.
:smiling_face::+1:

2 Likes

Here are complicated regex’s allowing you to add alternatives (either this|or this) in the find. Unfortunately there’s no easy way to add alternatives to the replace. Each line is handled separately so there is a lot of duplication. This assumes the source data will always follow the same structure. It would be good to get examples over time to handle any differences in the source data.

Assuming global variable is called theData

$tc(reg, gv(theData), 

".*(Saldo:) (\d*+(?:\.\d+)+) (CUP)(?:.*\s)*.*", 

"-$1 $2 $3"

)$$tc(reg, gv(theData), 

"(?:.*\s)*(Datos:) (\d*+(?:\.\d+)+) (GB) \+ (\d*+(?:\.\d+)+) (GB) (LTE)(?:.*\s)*.*", 

"
-$1 $2 $3
-$6: $4 $5"

)$$tc(reg, gv(theData), 

"(?:.*\s)*(Voz:) (\d*+:\d+(?::\d+)?)(?:.*\s)*.*", 

"
-$1 $2"

)$$tc(reg, gv(theData), 
   "(?:.*\s)*(SMS:) (\d*+)(?:.*\s)*.*", 
   "
-$1 $2"

)$$tc(reg, tc(cap,gv(theData)), 

"(?:.*\s)*(Activa)[^\d]+(\d*+(?:-\d+)+)(?:.*\s)*.*", 

"
-$1: $2"

)$$tc(reg, tc(cap,gv(theData)), 

"(?:.*\s)*(Vence)[^\d]+(\d*+(?:-\d+)+)(?:.*\s)*.*", 

"
-$1: $2"

)$

@dmtamayo92 :

I have modified my code snippet in order to give a much similar result as you want.

I hope this would help.
:smiling_face::+1: