Regex with multiple values text

I need help getting create a variable using REGEX.
I need to get the two characters that come after the text “Estado :”, but in the body of my text it appears 3 times “Estado :”

How do I get the second or third value? Sample text:

Bairro : Vila Maria Baixa Cidade : São Paulo Estado : SP Cep : 02117-000
Est. Civil : Solteiro(a) Nat. : IBIRA Estado : RJ Nacionalidade  : Brasileira
Endereço : AV  ANTENOR NAVARRO Nº  : 1067 Compl. :
Bairro : JARDIM BRASIL Cidade : SÃO PAULO Estado : PR Cep : 02224-001

I tried this REGEX but it only returns the first result

(?<=Estado :\s)\w\w


Welcome to our UiPath community.

Use Matches activity and it will return all successful matches from given string. And then you can take second and third occurance.

1 Like

assuming txt is the variable containing your sample text,

System.Text.RegularExpressions.Regex.Matches(txt, "(?<=Estado :\s)\w\w")(0).Value returns the first result i.e. SP

System.Text.RegularExpressions.Regex.Matches(txt, "(?<=Estado :\s)\w\w")(1).Value returns the 2nd result RJ

System.Text.RegularExpressions.Regex.Matches(txt, "(?<=Estado :\s)\w\w")(1).Value returns the 3rd result PR

or you just use a loop to print out all the results

Error compiling first suggestion

Class ‘System.Text.RegularExpressions.Match’ cannot be indexed because it has no default properties

Any suggestion ?

Hi @Fernando_Ferrari_Longato

Instead of using Match try to use Matches


@Fernando_Ferrari_Longato - Is this what you are looking for??

1 Like

Worked, thank you

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.