I'm having trouble finding a clear tutorial on how to use regular expressions, and its driving me insane..

However, based on an example I saw, I am trying to construct my own pattern and I cannot figure out why it is not working. Can somebody here give me some help?

I have a string that begins with "Data Source=" and I want to parse the data that comes after the = and before the ; at the end. But I also want to split the string into a path/filename pair (so I can store them in separate variables)

The code I have is as follows, but doesn't match anything.

string data = @"data source=c:\blah\file.ext;some more options=...;";
            string regex = @"data source=(^.*\\)([A-Za-z0-9\-]+\.[A-Za-z0-9]+)$";
            Match asdf = Regex.Match(data, regex, RegexOptions.IgnoreCase);
            Console.WriteLine(asdf.Groups[1].Value);
            Console.WriteLine(asdf.Groups[2].Value);

Recommended Answers

All 2 Replies

Ok I have been looking at this regexp for a few minutes now and I don't know a lot about regular expressions, but here's what I think you might want to try:

string regex = @"^data source=(.*\\)([A-Za-z0-9\-]+\.[A-Za-z0-9]+);.*$";

You've got a ^ in your first group, which either says that you want the middle of the string to be at the beginning of the string or that you don't want there to be a .*\\ after data source= and I don't see a ; anywhere in your expression, so since you're looking for just alphanumeric characters after the . (of .ext) up to the end of the string, you won't find it.

you could still leave out the .*$ at the end, since saying that this doesn't have to be found near the end is the same as saying any number of anything up to the end as of here

That worked like a charm. Thanks a ton.

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.