0

我想匹配两个字符串之间的正则表达式。

输入文本是这样的:

 Back to previous › 






             › Send Message 

                             › Add as Buddy 
             › Add as Favorite 
             › Block this Person 





         People who like this (click to upvote) 

我想匹配Back to previous >People who like this (click to upvote)之间的所有内容。

我尝试了最简单的正则表达式,(?<=\ Back\ to\ previous\ ›\ ).*(?=People\ who\ like\ this\ profile\ \(click\ to\ upvote\)\ )但没有运气。

想法是捕获 2 行\字符串之间的所有内容,即使您捕获的是换行符、制表符、字母数字等。

4

2 回答 2

1

试试这个正则表达式:

(?<=Back\sto\sprevious.*?›).?(?(?=People\swho\slike\sthis)

string Input = @"Back to previous › 






         › Send Message 

                         › Add as Buddy 
         › Add as Favorite 
         › Block this Person 





     People who like this (click to upvote) ";
        foreach (Match M in Regex.Matches(Input, @"(?<=Back\sto\sprevious.*?›).*?(?=People\swho\slike\sthis)", RegexOptions.IgnoreCase | RegexOptions.Singleline))
        {
            MessageBox.Show(M.Value.Trim());
        }

这将在消息框中显示以下内容:

› Send Message 



                         › Add as Buddy 

         › Add as Favorite 

         › Block this Person
于 2012-09-23T13:07:18.487 回答
0

如果您确定在不同的行上有字符串分隔符(例如“返回上一个”),那么您没有理由使用正则表达式:

string text = /* Get Text */;
string lines = text.Split();
IEnumerable<string> content = lines.Skip(1).Take(lines.length - 2);

或者:

const string matchStart = "Back to previous >";
const string matchEnd = "People who like this (click to upvote)"
int beginIndex = text.IndexOf(matchStart) + matchStart.Length;
int endIndex = text.IndexOf(matchEnd);
string content = text.Substring(beginIndex, endIndex - beginIndex);

(我发布的代码未经测试,但它应该可以工作)

于 2012-09-23T12:56:59.590 回答