正则表达式搜索字符串?

时间:2017-12-09 03:31:14

标签: c# regex

我想要匹配的字符串将始终包含短语" START"。我怎样才能返回包含此短语的匹配项?

这是我目前的模式:

"(?<=utm_name=potato\" rel=(\"carrot\"|\"*\") >)(.*?)(?=</a>)"

这是字符串输入:

&amp;utm_name=potato" rel="" >[START] This is a sentence.</a>&#32;<span class="beans">&amp;utm_name=potato" rel="carrot" >[START] This is another sentence.</a>&#32;<span class="beans">&amp;utm_name=potato" rel="carrot" >[FALSE] Do not match this sentence.</a>&#32;<span class="beans">

期望的输出:

[START] This is a sentence.

[START] This is another sentence.

当前输出:

[START] This is a sentence.

[START] This is another sentence.

[FALSE] Do not match this sentence.

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是使用.IndexOf()在字符串上传递搜索参数。

首先创建一个int来保存搜索词组的索引值,以及您认为该词组将要结束的位置的索引值。

您想要的子字符串位于初始搜索短语的索引和上一个搜索短语的索引之间。在这种情况下,最后一个短语在另一个搜索字符串上结束,即。即</a>

string test = "&amp;utm_name=potato\" rel=\"\" >[START] This is a sentence.</a>&#32;<span class=\"beans\">&amp;utm_name=potato\" rel=\"carrot\" >[START] This is another sentence.</a>&#32;<span class=\"beans\">&amp;utm_name=potato\" rel=\"carrot\" >[FALSE] Do not match this sentence.</a>&#32;<span class=\"beans\">";

string searchString = ">[START] ";  //start after this
string endSearchString = "</a>"; //end before this

确保在创建初始搜索词组的索引时,添加该搜索词组的长度。

//find the index of them within the sample test string
int indexOfInitial = test.IndexOf(searchString) + searchString.Length;
int indexOfEnding = test.IndexOf(endSearchString);

为新子字符串创建长度。这将是由最初搜索短语的索引减去的最后一个搜索短语的索引的值。您现在拥有创建子字符串所需的内容。

//create a length for substring
int length = indexOfEnding - indexOfInitial;
//create desired substring out of search string and length between start and end phrases
string desiredSubstring = test.Substring(indexOfInitial, length);

有关如何使用字符串输入的示例,请参阅代码演示:

static void Main(string[] args)
{
    //create sample test string
    string test = "&amp;utm_name=potato\" rel=\"\" >[START] This is a sentence.</a>&#32;<span class=\"beans\">&amp;utm_name=potato\" rel=\"carrot\" >[START] This is another sentence.</a>&#32;<span class=\"beans\">&amp;utm_name=potato\" rel=\"carrot\" >[FALSE] Do not match this sentence.</a>&#32;<span class=\"beans\">";
    //create strings you are searching for
    string searchString = ">[START] ";
    string endSearchString = "</a>";

    Console.WriteLine("String:");
    Console.WriteLine(test);

    //find the index of them within the sample test string
    int indexOfInitial = test.IndexOf(searchString) + searchString.Length;
    int indexOfEnding = test.IndexOf(endSearchString);

    //create a length for substring
    int length = indexOfEnding - indexOfInitial;

    string desiredSubstring = test.Substring(indexOfInitial, length);

    //desired result 1
    Console.WriteLine("\nResult 1:\n\n{0}", desiredSubstring);

    //Take of everything from the indexOfEnding
    test = test.Substring(indexOfEnding+endSearchString.Length);
    Console.WriteLine("\nString:\n{0}\n",test);    

    //change the search strings if needed
    //endSearchString = "</ a >"; //notice the spacing changing

    //find the index of them again within the new substring
    indexOfInitial = test.IndexOf(searchString) + searchString.Length;
    indexOfEnding = test.IndexOf(endSearchString);
    //create a length for substring
    length = indexOfEnding - indexOfInitial;
    //desired result 2
    Console.WriteLine("\nResult 2:\n\n{0}", test.Substring(indexOfInitial, length));
    Console.ReadKey();
}