簡體   English   中英

在C#中使用正則表達式提取某些子字符串

[英]Extract certain substring using regex in c#

我有一個單詞,其中有一個說1.2.2some text然后加上some other texts 我想獲得該部分。 我創建了一個正則表達式來匹配該部分和一些文本。

下面是我的代碼:

var word = "1.2.3 area consent testing, sklfjsdlkf jdifgjds visjeflk area consent testing lsdajfgo idsjgosa jfikdjfl343 fjdsl45jl sfgjsoiaetj l area consent testing";
var lowerWord = "area consent testing".ToLower();
var textLower = @word.ToLower().ToString();
Dictionary<int, string> matchRegex = new Dictionary<int, string>();
matchRegex.Add(1, @"(^\d.+(?:\.\d+)*[ \t](" + lowerWord + "))"); 


foreach (var check in matchRegex)
{
    string AllowedChars = check.Value;
    Regex regex = new Regex(AllowedChars);
    var match = regex.Match(textLower);
    if (match.Success)
    {
        var sectionVal = match.Value;
    }
}

現在我的問題是,我只想在我的sectionVal變量中使用值1.2.3 area consent testing ,但這實際上是整行代碼。

sectionVal = "1.2.3 area consent testing, sklfjsdlkf jdifgjds visjeflk area consent testing lsdajfgo idsjgosa jfikdjfl343 fjdsl45jl sfgjsoiaetj l area consent testing";

正則表達式的開頭包含一個未轉義的. 它將匹配任何字符,后跟+ 嘗試這個:

@"^(\d+(\.\d+)*[ \t](" + lowerWord + "))"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM