简体   繁体   English

RegEx用于从字符串中提取数字

[英]RegEx for extracting number from a string

I have a bunch of files in a directory, mostly labled something like... 我在目录中有一堆文件,大部分都贴上了类似...

PO1000000100.doc or .pdf or .txt Some of them are PurchaseOrderPO1000000109.pdf PO1000000100.doc或.pdf或.txt其中一些是PurchaseOrderPO1000000109.pdf

What i need to do is extract the PO1000000109 part of it. 我需要做的是提取其中的PO1000000109部分。 So basically PO with 10 numbers after it... How can I do this with a regex? 所以基本上PO后面是10个数字...我该如何使用正则表达式呢?

(What i'll do is a foreach loop on the files in the directory, get the filename, and run it through the regex to get the PO number...) (我要做的是在目录中的文件上进行一个foreach循环,获取文件名,然后通过正则表达式运行它以获取采购订单编号...)

I'm using C# - not sure if this is relevant. 我正在使用C#-不知道这是否相关。

Try this 尝试这个

String data = 
  Regex.Match(@"PO\d{10}", "PurchaseOrderPO1000000109.pdf", 
    RegexOptions.IgnoreCase).Value;

Could add a Regex.IsMatch with same vars above ofc :) 可以在Ofc之上添加具有相同变量的Regex.IsMatch :)

If the PO part is always the same, you can just get the number without needing to use a regex: 如果PO部分始终相同,则无需使用正则表达式即可获取数字:

new string(theString.Where(c => char.IsDigit(c)).ToArray());

Later you can prepend the PO part manually. 稍后,您可以手动添加PO部分。

NOTE : I'm assuming that you have only one single run of numbers in your strings. 注意 :我假设您的字符串中只有一个数字。 If you have for example "abc12345def678" you will get "12345678" , which may not be what you want. 例如,如果您有"abc12345def678" ,则将获得"12345678" ,这可能不是您想要的。


Regex.Replace(fileName, @"^.?PO(\d{10}).$", "$1");
将星星放在圆点后面。

string data="PurchaseOrderPO1000000109.pdf\nPO1000000100.doc";
MatchCollection matches = Regex.Matches(data, @"PO[0-9]{10}");
foreach(Match m in matches){
    print(m.Value);
}

Results 结果

PO1000000109
PO1000000100

This RegEx will pick up all numbers from a string \\d* . 此RegEx将从字符串\\d*提取所有数字。

As described here . 如上所述这里

可能的正则表达式可能是:

^.*(\d{10})\.\D{3}$
var re = new System.Text.RegularExpressions.Regex("(?<=^PurchaseOrder)PO\\d{10}(?=\\.pdf$)");
Assert.IsTrue(re.IsMatch("PurchaseOrderPO1234567890.pdf"));
Assert.IsFalse(re.IsMatch("some PurchaseOrderPO1234567890.pdf"));
Assert.IsFalse(re.IsMatch("OrderPO1234567890.pdf"));
Assert.IsFalse(re.IsMatch("PurchaseOrderPO1234567890.pdf2"));

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

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