johnnyF56

Hi,

I am trying to filter text similar to the following

eg. 56, aa, bb, "cc, dd, ee, ff"

and get

56, aa, bb, "cc dd ee ff".

I've tried the following

Dim x As String = "56, aa, bb,""cc, dd, ee, ff"""

Dim re as New Regex(",(\s*\w+)")

x = re2.Replace(username,"$1")

msgbox(x)

but this only gives me

56 aa bb,"cc dd ee ff"




Re: Regular Expressions Regular Expressions

tonn

Hi Johnny,

You want to remove all comma's within quoted sections, right

It's a nested structure so a two step approach is the easiest. Here's a simple one:

Code Snippet

string evaluator(Match match)

{

return match.Value.Replace(",", "");

}

private void button1_Click(object sender, EventArgs e)

{

string input = @"56, aa, bb, ""cc, dd, ee, ff""";

string pattern = @"""[\w]+(,\s*\w+)+""";

string output = Regex.Replace(input, pattern, evaluator);

MessageBox.Show(input + System.Environment.NewLine + output);

}

Regards, Tonn





Re: Regular Expressions Regular Expressions

johnnyF56

Hi Tonn,

thanks very much.That worked perfectly. In vb.net it's slighlty different.

string output = Regex.Replace(input, pattern, evaluator);

is

dim output as string = Regex.Replace(input, pattern, addressof Evaluator)

I've added in extra symbols for non-alphanumeric characters, so the following line I've changed from

string pattern = @"""[\w]+(,\s*\w+)+""";

to

dim pattern as string = """[\w|\(|\)|\&|\.|\-|\+|\%]+(,\s*\w+|\(|\)|\&|\.|\-|\+|\%)+"""

Thanks again

Johnny






Re: Regular Expressions Regular Expressions

OmegaMan

Don't double escape the quote ( " ) use the regex hex escape of \x22 and if you need ( ' ) use \x27. Makes life soooo much easier.