[英]c# regex matches example
我正在嘗試從以下文本中獲取值。 這如何用 Regex 完成?
Lorem ipsum dolor sat %download%#456 amet, consectetur adipiscing %download%#3434 elit。 Duis non nunc nec mauris feugiat porttitor。 sed tincidunt blandit dui a viverra%download%#298。 Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit。
456
3434
298
893434
因此,您試圖獲取以標記“%download%#”開頭的數值?
試試這個模式:
(?<=%download%#)\d+
那應該工作。 我不認為#
或%
是 .NET Regex 中的特殊字符,但是您必須像\\\\
一樣轉義反斜杠,或者對整個模式使用逐字字符串:
var regex = new Regex(@"(?<=%download%#)\d+");
return regex.Matches(strInput);
在這里測試: http : //rextester.com/BLYCC16700
注意: lookbehind 斷言(?<=...)
很重要,因為您不想在結果中包含%download%#
,只包含它后面的數字。 但是,您的示例似乎在您要捕獲的每個字符串之前都需要它。 后視組將確保它存在於輸入字符串中,但不會將其包含在返回的結果中。 更多關於環視斷言的信息在這里。
我看到的所有其他響應都很好,但 C# 支持命名組!
我會使用以下代碼:
const string input = "Lorem ipsum dolor sit %download%#456 amet, consectetur adipiscing %download%#3434 elit. Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.";
static void Main(string[] args)
{
Regex expression = new Regex(@"%download%#(?<Identifier>[0-9]*)");
var results = expression.Matches(input);
foreach (Match match in results)
{
Console.WriteLine(match.Groups["Identifier"].Value);
}
}
代碼如下: (?<Identifier>[0-9]*)
指定[0-9]*
的結果將是我們如上索引的命名組的一部分: match.Groups["Identifier"].Value
public void match2()
{
string input = "%download%#893434";
Regex word = new Regex(@"\d+");
Match m = word.Match(input);
Console.WriteLine(m.Value);
}
看起來這里的大部分帖子都描述了你在這里需要的東西。 但是 - 您可能需要更復雜的行為 - 取決於您要解析的內容。 在您的情況下,您可能不需要更復雜的解析 - 但這取決於您要提取的信息。
您可以在類中使用正則表達式組作為字段名稱,之后可以這樣寫:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
public class Info
{
public String Identifier;
public char nextChar;
};
class testRegex {
const string input = "Lorem ipsum dolor sit %download%#456 amet, consectetur adipiscing %download%#3434 elit. " +
"Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.";
static void Main(string[] args)
{
Regex regex = new Regex(@"%download%#(?<Identifier>[0-9]*)(?<nextChar>.)(?<thisCharIsNotNeeded>.)");
List<Info> infos = new List<Info>();
foreach (Match match in regex.Matches(input))
{
Info info = new Info();
for( int i = 1; i < regex.GetGroupNames().Length; i++ )
{
String groupName = regex.GetGroupNames()[i];
FieldInfo fi = info.GetType().GetField(regex.GetGroupNames()[i]);
if( fi != null ) // Field is non-public or does not exists.
fi.SetValue( info, Convert.ChangeType( match.Groups[groupName].Value, fi.FieldType));
}
infos.Add(info);
}
foreach ( var info in infos )
{
Console.WriteLine(info.Identifier + " followed by '" + info.nextChar.ToString() + "'");
}
}
};
此機制使用 C# 反射來為類設置值。 組名與類實例中的字段名匹配。 請注意 Convert.ChangeType 不接受任何類型的垃圾。
如果要添加行/列的跟蹤 - 您可以為行添加額外的 Regex 拆分,但為了保持 for 循環完整 - 所有匹配模式都必須具有命名組。 (否則列索引會計算錯誤)
這將導致以下輸出:
456 followed by ' '
3434 followed by ' '
298 followed by '.'
893434 followed by ' '
Regex regex = new Regex("%download#(\\d+?)%", RegexOptions.SingleLine);
Matches m = regex.Matches(input);
我認為可以解決問題(未測試)。
這種模式應該有效:
#\d
foreach(var match in System.Text.RegularExpressions.RegEx.Matches(input, "#\d"))
{
Console.WriteLine(match.Value);
}
(我不在 Visual Studio 前面,但即使它不能按原樣編譯,它也應該足夠接近以調整為有效的東西)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.