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
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
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}")