tags:

views:

87

answers:

5
+5  Q: 

Regex C# problem

I'm sure there is a simple solution, but I just seem to be missing it.

I need a regex to do the following:

asdf.txt;qwer should match asdf.txt

"as;df.txt";qwer should match as;df.txt

As you can see, I need to match up to the semi-colon, but if quotes exist(when there is a semi-colon in the value), I need to match inside the quotes. Since I am looking for a file name, there will never be a quote in the value.

My flavor of regex is C#.

Thanks for your help!

A: 

Do you need to use regex, you could just use the C# string method contains:

string s = "asdf.txt;qwer";
string s1 = "\"as;df.txt\";qwer";

return s.Contains("asdf.txt"); //returns true
return s1.Contains("\"as;df.txt\""); //returns true
Dustin Laine
A: 

Hi,

If you are looking for the two explicit strings asdf.txt and as;df.txt these can then simply be your two regex epressions. So something like the following.

Matches matches = Regex.Matches('asdf.txt;qwer',"asdf.txt");

and

Matches matches = Regex.Matches('"as;df.txt";qwer',"as;df.txt");

Enjoy!

Doug
they are not those specific strings. They could be any valid file name
Seattle Leonard
+1  A: 

This should do you:

(".*"|.*);

It technically matches the semicolon as well, but you can either chop that off or just use the backreference (if C# supports backreferences)

OmnipotentEntity
+1  A: 

This will match up to a ; if there are no quotes or the quoted text followed by a ; if there are quotes.

("[^"]+";|[^;]+;)
Sam
This match will include the semicolon.
Doug D
+1  A: 
"[^"]+"(?=;)|[^;]+(?=;)

This matches text within double quotes followed by a semicolon OR text followed by a semicolon. The semicolon is NOT included in the match.

EDIT: realized my first attempt will match the quotes. The following expression will exclude the quotes, but uses subexpressions.

"([^"]+)";|([^;]+);
Doug D
I've never used that ?= notation. What is it called and what flavors support it?
Seattle Leonard
It is 'look-ahead equals'. It's supported by at least .NET and JavaScript. My favorite site to test regular expressions is http://www.regexlib.com/RETester.aspx. It supports multiple engines, including JavaScript and .NET.
Doug D
You got me on the right path. Here is the regex I chose `(?<=")?[^"]+(?="?;)`
Seattle Leonard
Awesome! I was just looking for the 'look-behind' (?<=), which I suspect is .NET only. I don't see it in my JavaScript reg exp reference.
Doug D
I use a tool called Expresso which is free. It breaks your regex into a tree structure so you can pick it apart easier
Seattle Leonard
I had to tweek it a little, because my string had another ; in it. Here is the final version `(?<=^("?))[^"]+?(?=\1;)`
Seattle Leonard