Find a character and digits after these characters in a string

Hi everybody,

I need to extract a chain of character of a string.

For example this one : “LA SUPPLEMENTAIRE )LM RS RENTE PF161256980000-1589656-MARIE MADELEINE 20220401 LA MON”

I want to extract only the “PF” + 9 digits so i used Regex.Match(MyString, “PF\d{9}”).Value

The problem is that sometimes i have many “PF” in the string and if the string contains PF and nothing after i have this error message “startIndex cannot be larger than length of string”

So my question is :

How can i make an “IF” condition where i can find this pattern

“PF + 9 digits”

Thank you

Best regards

Guillaume

Hi @guillaume.cabanes2 ,

I believe you could use the IsMatch method at first to check the value actually exists, if so then use the Match method provided above.

The IsMatch could be used in the below way in an If Activity :

Regex.IsMatch(MyString, "PF\d{9}")

The above will return a boolean output.

Another approach would be to check if the Resultant string from the Match method is Empty or not like the below :

String.IsNullOrWhiteSpace(Regex.Match(MyString, "PF\d{9}").Value)
1 Like

Hey before using the regex you can chech if your input string where you want to search contains that PF character you can use string.contains if its true then you can extract using the regex


or
boolVar = System.Text.RegularExpressions.Regex.IsMatch(MyString, “(?<=PF)\d{9}”) This will give true or false then give if condition in true assign the strVar = System.Text.RegularExpressions.Regex.Match(MyString,“(?<=PF)\d{9}”).value

1 Like

Hi,

In this case, it might be better to use Match type (System.Text.RegularExpressions.Match) as the following, because we can get match value by one regex process.

m = System.Text.RegularExpressions.Regex.Match(MyString, "PF\d{9}")

Then check ,m.Success

Regards,

1 Like

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