我的问题是我将要构建一个函数来检索 PDF 文档并打印我想要获取以 . 开头的指定数字的文档8
。
输入:
"hello world, the number I want call is 84553741. help me plz."
正则表达式:
String[] result = Regex.Split(Result, @"[^\d$]");
如何找到以数字开头的数字8
?
已经可用的两个答案实际上并不匹配以 8开头的数字,而是匹配包含8 的数字。但是,匹配将从 8 开始。
要仅匹配以 8 开头的数字,您需要此正则表达式:
string[] testArray = new string[] { "test888", "test181", "test890", "test8" };
Regex regex = new Regex(@"(?<!\d)8\d*");
foreach (string testString in testArray)
{
if (regex.IsMatch(testString))
Console.WriteLine("\"{0}\" matches: {1}", testString, regex.Match(testString));
else
Console.WriteLine("\"{0}\" doesn't match", testString);
}
输出将是:
"test888" matches: 888
"test181" doesn't match
"test890" matches: 890
"test8" matches: 8
使用正则表达式"8\d*"
将产生以下结果:
"test888" matches: 888 // not mentioned results: 88 and 8
"test181" matches: 81 // obviously wrong
"test890" matches: 890
"test8" matches: 8
以下代码将从提供的输入字符串中提取所有以 8 开头的数字。
var input= "hello world, the number i want call is 84553741. help me plz.";
var matches = Regex.Matches(input, @"(?<!\d)8\d*");
IEnumerable<String> numbers = matches.Cast<Match>().Select(m=>m.Value);
foreach(var number in numbers)
{
Console.WriteLine(number);
}