6

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!

1
  • Can you have escaped quotes (like \" or "") inside the quotes? Commented Oct 27, 2010 at 17:29

5 Answers 5

2
"[^"]+"(?=;)|[^;]+(?=;)

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.

"([^"]+)";|([^;]+);
Sign up to request clarification or add additional context in comments.

6 Comments

I've never used that ?= notation. What is it called and what flavors support it?
It is 'look-ahead equals'. It's supported by at least .NET and JavaScript. My favorite site to test regular expressions is regexlib.com/RETester.aspx. It supports multiple engines, including JavaScript and .NET.
You got me on the right path. Here is the regex I chose (?<=")?[^"]+(?="?;)
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.
I use a tool called Expresso which is free. It breaks your regex into a tree structure so you can pick it apart easier
|
1

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)

Comments

1

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

("[^"]+";|[^;]+;)

1 Comment

This match will include the semicolon.
0

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

Comments

0

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!

1 Comment

they are not those specific strings. They could be any valid file name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.