简体   繁体   English

如何解析字符串以生成特定的字母数字格式

[英]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. P1234567。 which I would like to fetch ***Starts with letter P followed by 7 digits我想获取 ***以字母 P 开头,后跟 7 位数字


Examples:例子:

  1. "P1000000:P1000003:P1000002" “P1000000:P1000003:P1000002”
  2. *"P1300001,P1300002" *“P1300001,P1300002”
  3. "P2000001 CUSTOMER_A" “P2000001 客户_A”
  4. "P3000001P3000002P3000003P3000004" “P3000001P3000002P3000003P3000004”
  5. "VehicleREG P4000001 Additional DocP9876543" “VehicleREG P4000001 附加 DocP9876543”

I am able to split No. 1,2 and some instances of 3 using regex我可以使用正则表达式拆分 1,2 号和 3 的一些实例
How can I parse no.4 & 5 to fetch and split my format specific strings如何解析 no.4 和 5 以获取和拆分我的格式特定字符串

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如果,无论他们输入什么其他垃圾,如果您的用户可靠地输入一个 P 后跟 7 位数字,您可以使用

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 mc 将是一个 MatchCollection,其中一个或多个 Match 对象具有.Groups[0].Value即 P 编号

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如果需要,您可以更多地参与其他解析 - 我切换到使用命名捕获组( (?<namehere>... )定义了一个命名组),因为当您有多个捕获时,使用名称而不是更容易使它们保持直接数字索引

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如果您发现您的用户有时会打错数字并输入例如 6 到 8 位数字,您可以将正则表达式调整为\d{6,8} ,然后通过填充或修剪将其在代码中修复为 7 位数字作为适当的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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