andrei miko
andrei miko

Reputation: 21

How can I match this with a regex ? C#

I wanna match with a regex everything between 2 strings.

The input text is like this:

 Back to previous › 






             › Send Message 

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





         People who like this (click to upvote) 

I wanna match everything between Back to previous > and People who like this (click to upvote).

I tried the easiest regex which is (?<=\ Back\ to\ previous\ ›\ ).*(?=People\ who\ like\ this\ profile\ \(click\ to\ upvote\)\ ) but no luck with it.

The ideea is to catch everything between 2 lines\strings, even thought what you catch is newline, tab, alphanumeric, and so on.

Upvotes: 0

Views: 279

Answers (2)

as-cii
as-cii

Reputation: 13019

If you are sure you have your string delimiters (e.g. "Back to Previous") on different lines you have no reason of using Regexes:

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

Or alternatively:

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);

(The code I've posted is not tested but it should work)

Upvotes: 0

DelegateX
DelegateX

Reputation: 719

Try this Regex:

(?<=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());
        }

This displays following in the message box:

› Send Message 



                         › Add as Buddy 

         › Add as Favorite 

         › Block this Person

Upvotes: 1

Related Questions