[英]C# Regex - Extract multiple parts of string
在 C# 中,我試圖提取以下格式的字符串的多個部分(粗體和斜體)。 請注意,數字可能長 6-7 位,因此不需要對長度進行任何限制,但是粗斜體字符串的第一部分始終為 3 個字符長。
TCS- TST .MSL-M365-SPO.S 8629 -O 2887 .Engagement
目前我正在使用 string.Split() ,它需要多次拆分字符串並提取我需要的部分。 所以想知道是否有更好的方法來使用 REGEX。
嘗試一些在線工具。
^\w{3}-(?<First>\w{3}).\w{3}-\w\d+-\w{3}.\w(?<Second>\d+)-\w(?<Third>\d+).\w+$
這是一個好的開始,然后使用組名來提取值:
var regex = new Regex(@"^\w{3}-(?<First>\w{3}).\w{3}-\w\d+-\w{3}.\w(?<Second>\d+)-\w(?<Third>\d+).\w+$");
var match = regex.Match(input);
if (!match.Success)
return;
var first = match.Groups["First"];
var second = match.Groups["Second"];
var third = match.Groups["Third"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.