[英]C# how to write Regular Expression
我的文件中包含某些數據,例如:
/頁2 0 R /類型/目錄/ AcroForm
/ Count 1 / Kids [3 0 R] / Type / Pages
/篩選器/ FlateDecode /長度84
獲得此輸出的正則表達式是什么。
Pages Type Catalog AcroForm Count Kids Type Pages Filter FlateDecode Length
我想在'/'之后和第二個'/'或空格之前獲取字符串。
提前致謝。
class Program
{
static void Main()
{
string s = @"/Pages 2 0 R/Type /Catalog/AcroForm
/Count 1 /Kids [3 0 R]/Type /Pages
/Filter /FlateDecode/Length 84";
var regex = new Regex(@"[\/]([^\s^\/]*)[\s]");
foreach (Match item in regex.Matches(s))
{
Console.WriteLine(item.Groups[1].Value);
}
}
}
備注:不要使用正則表達式來解析PDF文件。
\/[^\/\s]+
\\/
-斜線(轉義)
[^ ]
-不(A字符類^
)含...
\\/
-...斜線...
\\s
-...或空格
+
-其中一個或多個
這是針對c#的:
@"/([^\s/]+)"
您可以在此處進行測試,只需添加引號之間的內容即可: http : //derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
我不會為此使用正則表達式,但發現使用字符串操作更具可讀性:
string[] lines = input.split(@"\");
foreach(string line in lines)
{
if(line.contains(" "))
{
// Get everything before the space
}
else
{
// Get whole string
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.