471,337 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Regex String searching for quotes

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
Jul 11 '07 #1
3 3584
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/lib...fe(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

Jul 11 '07 #2
bob wrote:
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]*)";

--
Göran Andersson
_____
http://www.guffa.com
Jul 11 '07 #3
bob
Marc and Goran,
Thank you.
I went with the string literal.
regards
Bob
Jul 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by William Stacey [MVP] | last post: by
3 posts views Thread by Luis Esteban Valencia | last post: by
4 posts views Thread by JS | last post: by
9 posts views Thread by jmchadha | last post: by
2 posts views Thread by Neeta | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.