我有以下字符串
29 This is a Page1 6754001 1,2,3,4
6755 This is a Page2 56-0 7654564
This is a Page3 67543-986xx 8 12
This is (Page5)& Container 876-0 6 8xp从上面,我需要提取下面的文本
This is a Page1
This is a Page2
This is a Page3
This is (Page5)& Container在第一个数字和文本之间总是有一个空格,所以在2129之间有一个空格,这是page1。有时第一个数字被省略,就像2129消失了一样。文本和下一个数字之间总是有一个空格,所以在Page1和6754001之间有一个空格,有时可以有两个空格。我只需要提取这些线--这些线总是在空格之后开始的,这样就可以
29 This is page1它们总是被一个空间所取代,有时是一个空间,有时是两个空间。
任何帮助都将不胜感激。
发布于 2014-09-18 16:07:13
^\d*.*?\s+|(?<=\s)\d{2,}.*(?=\s|$)尝试this.This将使用您最新的requriement.See演示程序
http://regex101.com/r/gG5fF6/4
发布于 2014-09-18 04:45:57
您可以尝试下面的regex来获得文本,在文本的开头有一个可选的数字,后面跟着一个或多个空格和一个数字。
Regex:
^(?:\d+)?\s*(.*?)\s+\d.*替换字符串:
$1演示
通过更换字符串,
码
string str = @"29 This is a Page1 6754001 1,2,3,4
6755 This is a Page2 56-0 7654564
This is a Page3 67543-986xx 8 12
This is (Page5)& Container 876-0 6 8xp";
string result = Regex.Replace(str, @"(?m)^(?:\d+)?\s*(.*?)\s+\d.*", "$1");
Console.WriteLine(result);
Console.ReadLine();输出:
This is a Page1
This is a Page2
This is a Page3
This is (Page5)& Container依佩恩
或
通过Matches方法。
string str = @"29 This is a Page1 6754001 1,2,3,4
6755 This is a Page2 56-0 7654564
This is a Page3 67543-986xx 8 12
This is (Page5)& Container 876-0 6 8xp";
Regex rgx = new Regex(@"(?m)^(?:\d+)?\s*(.*?)\s+\d.*");
foreach (Match m in rgx.Matches(str))
Console.WriteLine(m.Groups[1].Value);依佩恩
https://stackoverflow.com/questions/25904265
复制相似问题