简体   繁体   中英

How can I parse string to produce specific alphanumeric format

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:

  1. "P1000000:P1000003:P1000002"
  2. *"P1300001,P1300002"
  3. "P2000001 CUSTOMER_A"
  4. "P3000001P3000002P3000003P3000004"
  5. "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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM