简体   繁体   中英

Regex Expression: Capture the Text Between the Lines which includes Newline, Spaces, And Underscore characters

I have some text taken from a pdf file and read into a string:

...

Fabric Business Of the Cloths 

4 Description of the property being purchased 
______________________________________________________________________________

...

I want to extract the words before the line 4 Description of the property being purchased and not anything above it or the underscore line below it.

I tried using the regex /^[^4]*/ but this is returning null.

What would be a suitable regex to achieve the above?

Thanks.

Your regex expression works, just remove the / at beginning and end.

EXAMPLE

    private void TestRegex()
    {
        string s = "...\n Fabric Business Of the Cloths\n                         4 Description of the property being purchased\n____________________________________________________________________________\n ...";
        Regex regex = new Regex("^[^4]*"); // <--- DO LIKE THIS, PERHAPS.
        //Regex regex = new Regex("/^[^4]*/"); <----NOT THIS
        Match match = regex.Match(s, 0);
        if (match.Success)
        {
            Console.WriteLine(match.Value);
        }
    }

OUTPUT

...
 Fabric Business Of the Cloths

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM