Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX question

Status
Not open for further replies.

way2many

Technical User
Nov 28, 2003
139
US
Hi,
I have comma delimited files where rows look like this:

"NN0K",22,33,"5,4",2006
"HHUN",35,50,"N80",2006

How can I substitute comma in string "5,4" for "5-4" ?

Thanks
P.S. I'm using C#
 
Use the Replace method


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thank you for answering.

Would you be so kind to give me an example.

Please, note that I need to replace commas inside any fields with ""
So, this text file:

"NN0K",22,33,"5,4",2006
"H,KL,UN",35,50,"NFFS8,0FK",2006

should look like:

"NN0K",22,33,"5-4",2006
"H-KL-UN",35,50,"NFFS8-0FK",2006
 
Please, note that I need to replace commas inside any fields with ""
Ah, I see - your first post didn't say that. In that case you will need a Regular Expression.

I would suggest downloading "The Regulator" from as this will allow you to build and test your Regular Expressions.

Also, is a good resource.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thank so very much for the link!
I'll download "The Regulator".

So far I have found one solution which doesn't use regex and works for me:


string text1 = "\"This is, some text\", abc, def, \"How about, multiple, commas?\"";
string[] text2 = text1.Split(',');
string[] text3 = new string[text2.Length];
string[] text4 = null;
int i = 0;
bool inOpenQuote = false;

foreach (string text in text2)
{
if (inOpenQuote)
{
i--;
text3 = string.Concat(text3, ", ", text);
}
else
text3 = text;
i++;
if (text.Trim().StartsWith("\""))
{
inOpenQuote = true;
}
else if (text.Trim().EndsWith("\""))
{
inOpenQuote = false;
}

}

text4 = new string;
for (int index = 0; index < i; index++)
text4[index] = text3[index];

foreach (string text in text4)
MessageBox.Show (text)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top