开发者

C# how to write Regular Expression

开发者 https://www.devze.com 2022-12-14 05:23 出处:网络
My file has certain data like:: /Pages 2 0 R/Type /Catalog/AcroForm /Count 1 /Kids [3 0 R]/Type /Pages

My file has certain data like::

/Pages 2 0 R/Type /Catalog/AcroForm

/Count 1 /Kids [3 0 R]/Type /Pages

/Filter /FlateDecode/Length 84

What is the regular express开发者_如何学Pythonion to get this output..

Pages Type Catalog AcroForm Count Kids Type Pages Filter FlateDecode Length

I want to fetch string after '/' & before 2nd '/' or space.

Thanks in advance.


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);
        }

    }
}

Remark: Don't use regular expressions to parse PDF files.


\/[^\/\s]+

\/ -- A slash (escaped)
[^ ] -- A character class not (^) containing...
\/ -- ... slashes ...
\s -- ... or whitespace
+ -- One or more of these


Here it is for c#:

@"/([^\s/]+)"

You can test it here just adding what is in between quotes: http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx


I wouldn't use a regex for this, I find that using string operations is more readable:

string[] lines = input.split(@"\");
foreach(string line in lines)
{
    if(line.contains(" "))
    {
         // Get everything before the space
    }
    else
    {
         // Get whole string
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消