Hi Guys,
can anyone help me to write a regex function for the following string .So my string is

contents=false
book_title='The Old Man and His Sons'
sections=[
'The Old Man and His Sons'
]

now i want to extract just

The Old Man and His Sons

so far my code looks like this

 Match o = Regex.Match(item.text, "book_title='([A-Za-z0-9])'");

thanks a lot for all the help

Recommended Answers

All 5 Replies

Who knows what the title might contain, so capturing everything is a good idea. And a named group makes it easier to access the match.

using System;
using System.Text.RegularExpressions;

namespace Ed {
    public class Program {
        static void Main(string[] args) {
            string source =
                "contents=false\n" +
                "book_title='The Old Man and His Sons'\n" +
                "sections=[\n" +
                "'The Old Man and His Sons'\n" +
                "]";

            Console.WriteLine(Regex.Match(source, "book_title='(?<TITLE>.*)'").Groups["TITLE"]);
        }
    }
}

can you help me actually understand how this regex function work.I dont wana copy paste.Can you actually explain what ?<Title..*)' actually means.
thanks

> can you help me actually understand how this regex function work
Ed used ".*" to capture any number of characters instead of "[A-Za-z0-9]" to capture one instance of an alphabet letter or digit. There is also a named group, the "?<TITLE>" part, to make it easier to find the matched substring. You can use an unnamed group and access the Groups collection by index, but it is less convenient to find the one you want because there are other matched groups aside from the one in parentheses.

Match m = Regex.Match(source, "book_title='(.*)'");

// See all available groups
foreach (var group in m.Groups)
    Console.WriteLine(group);

// Get the substring group
Console.WriteLine(m.Groups[1]);

This is a handy reference.

Thanks for all the help i just have one more question for you.right now i am reading all the files from a folder which works fine.What i want to do now is to read the specific file from a folder.How can i do that .The path of the file should be selected by dialog box.Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.