简体   繁体   中英

Match with reges excluding string tags

I am trying to write a code in order to get the matches in a list but without the match tags, until now i have built the following code in WP7 application written on C#

public static MatchCollection MatchTags(string content, string string_start, string string_end)
{
    MatchCollection matches = Regex.Matches(content, string_start + "(.*?)" + string_end, RegexOptions.IgnoreCase);
    return matches;
}

So how to return matches without string_start, string_end (match tags) without use of replace function after the match extraction?

Use lookarounds..

String.Format("(?<={0}).*?(?={1})",string_start,string_end);

Though you can also use groups.ie in your regex (.*?) would capture the content within Group 1. No need of lookarounds then..

MatchTags(content,start,end).Cast<Match>()
                            .Select(x=>x.Groups[1].Value);

当我得到下一个代码的结果时,它起作用:

string my_string_no_tags = matches[number].Groups[1].Value;

Consider the following code...

MatchCollection matches = Regex.Matches(content, string.Format("(?<={0}).*?(?={1})", string_start, string_end), RegexOptions.IgnoreCase);
return matches;

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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