Regex String searching for quotes

B

bob

Hi
I have a long regex string that needs to match the quote char. It is
matching a CSV text file where all data fields are encased in quotes.
Some fields are empty ie double quotes. I want to make sure all fields
are correctly formed.
Is there any easy way to embed the '"" in the string or do I have to
break it up into something like
char quote = '"';
string myregex = "\\A" +quote + ",([^,\r\n]*)" + quote + "," + quote
+",([^,\r\n]*)" etc etc.
I formed the regex string in RegexBuddy and would love to just dump
into a c# string
thanks
Bob
 
M

Marc Gravell

You just need to escape it correctly... I think you need to read up on
string escaping (in both forms) - see http://msdn2.microsoft.com/en-us/library/362314fe(VS.80).aspx

Another option (especially useful for regex) is to make use of a
resource file; this allows you to store (named) strings, but without
any escaping at all - which will allow you to paste the regex verbatim
into the designer. You can then access this string at runtime via (for
instance) Resources.MyRegex.

Marc
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

bob said:
Hi
I have a long regex string that needs to match the quote char. It is
matching a CSV text file where all data fields are encased in quotes.
Some fields are empty ie double quotes. I want to make sure all fields
are correctly formed.
Is there any easy way to embed the '"" in the string or do I have to
break it up into something like
char quote = '"';
string myregex = "\\A" +quote + ",([^,\r\n]*)" + quote + "," + quote
+",([^,\r\n]*)" etc etc.
I formed the regex string in RegexBuddy and would love to just dump
into a c# string
thanks
Bob

You can use \" to put a quote in a string. You also have to escape the
backslash into \\.

A convenient string format for regular expression patterns is the @
delimited string. That turns off all escape sequences, and you use
double quotes to put a quote in the string:

string myregex = @"\\A"",([^,\r\n]*)"","",([^,\r\n]*)";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top