I have a reference field that is typed manually by multiple users
I have an alphanumeric document number ie. P1234567. which I would like to fetch ***Starts with letter P followed by 7 digits
Examples:
- "P1000000:P1000003:P1000002"
- *"P1300001,P1300002"
- "P2000001 CUSTOMER_A"
- "P3000001P3000002P3000003P3000004"
- "VehicleREG P4000001 Additional DocP9876543"
I am able to split No. 1,2 and some instances of 3 using regex
How can I parse no.4 & 5 to fetch and split my format specific strings
CodePudding user response:
If, regardless of the other garbage they type, if your users reliably type a P followed by 7 digits you can extract the info with
var mc = Regex.Matches(input, @"P\d{7}");
mc will be a MatchCollection with one or more Match objects having a .Groups[0].Value
that is the P number
If you want just the numeric part you can:
var mc = Regex.Matches(input, @"P(?<n>\d{7})");
foreach(Match m in mc)
Console.WriteLine(m.Groups["n"].Value);
You can get more involved with other parsing if you want - I switched to using named capturing groups ((?<namehere> ... )
defines a named group) because when you have multiple captures it's easier to keep them straight with names rather than numerical indexing
If you're finding that your users sometimes typo the digits and put eg between 6 and 8 digits in you can tweak the Regex to \d{6,8}
and then fix it up in code to be 7 digits by padding or trimming it as appropriate