473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help with replacement pattern

I'm looking to replace all commas in a single string not contained within a pair of double-quotes,
with a tab. I don't know where to begin.

e.g., string line = "a,",,"b" would be changed to "a,"\t\t"b"

Can someone suggest a regex pattern to use, or any other way that will accomplish this?

TIA Flomo
--

Aug 21 '07 #1
3 1433
I'm looking to replace all commas in a single string not contained
within a pair of double-quotes, with a tab. I don't know where to
begin.

e.g., string line = "a,",,"b" would be changed to "a,"\t\t"b"

Can someone suggest a regex pattern to use, or any other way that will
accomplish this?
You want to replace all commas preceeded by an even number of doublequotes.
Try using a look-behind pattern for that - e.g.
something like

str.replace(@"(?<=^([^""]*""[^""]*""[^""]*)*),", "\t");
>
TIA Flomo

Aug 21 '07 #2
Hello Niels,

Thank you for the suggestion.

Hello Flomo,

Niels's regex is the third method to resolve the problem. But this method
still suffers from the loss of performance.
I made a test to compare the three methods: (see the code listing 1)
The result is:
#direct operation on chars: 39ms.
#my regex: 6280ms.
#Niels's regex: 8997ms.
Therefore, I think the direct operation on chars is the best way by now.

Code Listing 1:
class Program
{
static void Main(string[] args)
{
string test = "\"a,,,,,\",\"j,dd,\"b\",\",\"";

Test1(test);
Test2(test);
Test3(test);
return;
}

public static void Test1(string test)
{
Stopwatch sw = new Stopwatch();
sw.Start();

for (int times = 0; times < 100000; times++)
{
char[] str = test.ToCharArray();
bool isInQuotes = false;
for (int i = 0; i < str.Length; i++)
{
if (!isInQuotes && str[i] == ',')
{
str[i] = '\t';
continue;
}
if (str[i] == '\"')
isInQuotes = !isInQuotes;
}
//Console.WriteLine(str);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

public static void Test3(string test)
{
Stopwatch sw = new Stopwatch();
sw.Start();

for (int times = 0; times < 100000; times++)
{
Regex regex = new Regex("(?<=^([^\"]*\"[^\"]*\"[^\"]*)*),");
regex.Replace(test, "\t");
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

public static void Test2(string test)
{
Stopwatch sw = new Stopwatch();
sw.Start();

for (int times = 0; times < 100000; times++)
{
Regex regex = new
Regex("(?<head>\".*?\")*(?<remove>,*)(?<tail>\".*? \")*");
MatchEvaluator myEvaluator = new
MatchEvaluator(Program.ReplaceFunction);
regex.Replace(test, myEvaluator);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

public static string ReplaceFunction(Match m)
{
return m.Groups["head"].Value +
m.Groups["remove"].Value.Replace(',', '\t') + m.Groups["tail"].Value;
}
}

Sincerely,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 21 '07 #3
Hi Flomo,

Would you mind letting me know the result of the suggestions? If you need
further assistance, feel free to let me know. I will be more than happy to
be of assistance.

Have a great day!

Sincerely,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sharif T. Karim | last post by:
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do. Text being searched: -- The brown fox...
2
by: Ruslan V. Sulakov | last post by:
The situation: root@oasis2# cat test.php ; echo "---------"; php test.php <?php $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}1,$3'; echo...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
18
by: talin at acm dot org | last post by:
I've been reading about how "lambda" is going away in Python 3000 (or at least, that's the stated intent), and while I agree for the most part with the reasoning, at the same time I'd be sad to see...
2
by: rattan | last post by:
I am trying to make prescript-2.2 (old python based psotscript to plain text converter). It gives the following dprecation message /local/users/ishwar/prescript-2.2/misc.py:23:...
5
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
3
by: Jon | last post by:
Hi... I'm getting stumped over the something that MUST be simple...basically, I'm trying to replace the characters 0D0A with a crlf, using Regex.Replace(). The problem is, the replaced string...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .=...
9
by: Simon Woods | last post by:
Hi I'm new to Regular Expressions so ... I trying to work out regular expressions to parse the following (a + (b + c)) I really want to replace it with
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.