By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

Regex with quotes

P: n/a
I am having difficulty writing a Regex constructor.

A line has a quote(") at its beginning and its end. I need to strip both characters off. If the
line looks like "1", I need the result to be 1.

If it were sed, I could just do s/^"// and s/"$//, but I'm confused with the quote escape
characters. Also, can I do both replacements at once?

I tried:

Regex regex = new Regex("[^""", """$], """""")

You can see I'm lost.

Thanks, Flomo
--

Aug 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello Flomo

The parenthesis is not a must. I added it so as to make the logic clearer.
You may remove the parenthesis:
Regex regex = new Regex("^\"|\"$");

Please feel free to let me know if you have any other concern.

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 17 '07 #2

P: n/a
Hello Jialiang Ge [MSFT],
Hello Flomo

The parenthesis is not a must. I added it so as to make the logic
clearer.
You may remove the parenthesis:
Regex regex = new Regex("^\"|\"$");
Please feel free to let me know if you have any other concern.
Usually when you have two regexes that you combine, but have nothign in common
(e.g. they do not logically follow eachother) it's faster to run them seperately.
Though these regexes are so short this advantage might nbe lost.

It might be even faster to just load the string in a stringbuilder and use
Remove on the first and the last character if needed. Even a simple substring
might be faster...

I tested all options with the code below and the end result was that with
reasonably short strings the Stringbuilder won, with larger strings the substring
function won. followed after a very large gat (almost 4x slower) by the regexes.

results in milliseconds for 10000 passes.
substring : 123
stringbuilder : 135
regexboth : 308
regextwopart : 443

I used compiled regexes and made sure the compile time was outside the stopwatch.

I've attached the code below, including the substring and stringbuilder funtions
I used

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections;

namespace ConsoleApplication1
{
public delegate string TestStringDelegate(string input);

class Program
{
static Dictionary<string, longresults = new Dictionary<string,
long>();

static Program()
{
replaceBoth.Match("");
replaceLeft.Match("");
replaceRight.Match("");
}

static void Main(string[] args)
{
string[] inputs = new string[]{
@"""........""",
@""".............................................. ..............................""",
@""".............................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ...........................""",
@""".............................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ...........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
.................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........................
"""
};

foreach (string input in inputs)
{
Test("substring", input, new TestStringDelegate(TestSubstring));
Test("stringbuilder", input, new TestStringDelegate(TestStringBuilder));
Test("regexboth", input, new TestStringDelegate(TestRegexBoth));
Test("regextwopart", input, new TestStringDelegate(TestRegexTwoPart));
}

WriteResults();
Console.ReadLine();
}

static void WriteResults()
{
foreach (string key in results.Keys)
{
Console.WriteLine("{0, -20}: {1}", key, results[key]);
}
}

static void Test(string name, string input, TestStringDelegate function
)
{
string output = "";
Stopwatch sw = new Stopwatch();
Console.WriteLine("Start test " + name);
sw.Start();
for (int i = 0; i < 10000; i++)
{
output = function(input);
}
sw.Stop();

if (results.ContainsKey(name))
{
long millis = results[name];
results[name] = millis + sw.ElapsedMilliseconds;
}
else
{
results.Add(name, sw.ElapsedMilliseconds);
}
Console.WriteLine(input);
Console.WriteLine(output);
Console.WriteLine(sw.ElapsedMilliseconds);
Console.WriteLine("End test " + name);
}

static string TestStringBuilder(string input)
{
StringBuilder sb = new StringBuilder(input);
if (sb.Length 0 && sb[0] == '\"')
{
sb.Remove(0, 1);
}
if (sb.Length 1 && sb[sb.Length - 1] == '\"')
{
sb.Remove(sb.Length - 1, 1);
}
string output = sb.ToString();
return output;
}

static string TestSubstring(string input)
{
int start = 0;
int end = input.Length;

if (input.StartsWith("\""))
{
start++;
}
if (input.EndsWith("\"") && input.Length 1)
{
end--;
}
string output = input.Substring(start, end - start);
return output;
}

static string TestRegexBoth(string input)
{
return replaceBoth.Replace(input, "");
}

static string TestRegexTwoPart(string input)
{
return replaceRight.Replace(replaceLeft.Replace(input, ""), "");
}

private static Regex replaceBoth = new Regex(@"^""|""$", RegexOptions.Compiled);
private static Regex replaceLeft = new Regex(@"^""", RegexOptions.Compiled);
private static Regex replaceRight = new Regex(@"""$", RegexOptions.Compiled);
}
}
--
Jesse Houwing
jesse.houwing at sogeti.nl

Aug 17 '07 #3

P: n/a
Jesse,

I really appreciate your analysis. I will change to StringBuilder.

Thanks again, Flomo
--

Jesse Houwing wrote:
Hello Jialiang Ge [MSFT],
Hello Flomo

The parenthesis is not a must. I added it so as to make the logic
clearer.
You may remove the parenthesis:
Regex regex = new Regex("^\"|\"$");
Please feel free to let me know if you have any other concern.

Usually when you have two regexes that you combine, but have nothign in common (e.g. they do not
logically follow eachother) it's faster to run them seperately. Though these regexes are so short
this advantage might nbe lost.

It might be even faster to just load the string in a stringbuilder and use Remove on the first
and the last character if needed. Even a simple substring might be faster...

I tested all options with the code below and the end result was that with reasonably short
strings the Stringbuilder won, with larger strings the substring function won. followed after a
very large gat (almost 4x slower) by the regexes.

results in milliseconds for 10000 passes.
substring : 123
stringbuilder : 135
regexboth : 308
regextwopart : 443

I used compiled regexes and made sure the compile time was outside the stopwatch.

I've attached the code below, including the substring and stringbuilder funtions I used

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections;

namespace ConsoleApplication1
{
public delegate string TestStringDelegate(string input);

class Program
{
static Dictionary<string, longresults = new Dictionary<string, long>();

static Program()
{
replaceBoth.Match("");
replaceLeft.Match("");
replaceRight.Match("");
}

static void Main(string[] args)
{
string[] inputs = new string[]{
@"""........""",

@""".............................................. ..............................""",
@""".............................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .................................""",
@""".............................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .................................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. .............................
.................................................. ................................................
.................................................. ................................................
.................................................. ................................................
.................................................. ............................. """
}; foreach (string input in inputs) {
Test("substring", input, new TestStringDelegate(TestSubstring));
Test("stringbuilder", input, new TestStringDelegate(TestStringBuilder));
Test("regexboth", input, new TestStringDelegate(TestRegexBoth));
Test("regextwopart", input, new TestStringDelegate(TestRegexTwoPart)); }

WriteResults();
Console.ReadLine();
}

static void WriteResults()
{
foreach (string key in results.Keys)
{
Console.WriteLine("{0, -20}: {1}", key, results[key]);
}
}

static void Test(string name, string input, TestStringDelegate function )
{
string output = "";
Stopwatch sw = new Stopwatch();
Console.WriteLine("Start test " + name);
sw.Start();
for (int i = 0; i < 10000; i++)
{
output = function(input);
}
sw.Stop();

if (results.ContainsKey(name))
{
long millis = results[name];
results[name] = millis + sw.ElapsedMilliseconds;
}
else
{
results.Add(name, sw.ElapsedMilliseconds);
}
Console.WriteLine(input);
Console.WriteLine(output);
Console.WriteLine(sw.ElapsedMilliseconds);
Console.WriteLine("End test " + name);
}

static string TestStringBuilder(string input)
{
StringBuilder sb = new StringBuilder(input);
if (sb.Length 0 && sb[0] == '\"')
{
sb.Remove(0, 1);
}
if (sb.Length 1 && sb[sb.Length - 1] == '\"')
{
sb.Remove(sb.Length - 1, 1);
}
string output = sb.ToString();
return output;
}

static string TestSubstring(string input)
{
int start = 0;
int end = input.Length;

if (input.StartsWith("\""))
{
start++;
}
if (input.EndsWith("\"") && input.Length 1)
{
end--;
}
string output = input.Substring(start, end - start);
return output;
}

static string TestRegexBoth(string input)
{
return replaceBoth.Replace(input, "");
}

static string TestRegexTwoPart(string input)
{
return replaceRight.Replace(replaceLeft.Replace(input, ""), "");
}

private static Regex replaceBoth = new Regex(@"^""|""$", RegexOptions.Compiled);
private static Regex replaceLeft = new Regex(@"^""", RegexOptions.Compiled);
private static Regex replaceRight = new Regex(@"""$", RegexOptions.Compiled);
}
}
--
Jesse Houwing
jesse.houwing at sogeti.nl
Aug 17 '07 #4

P: n/a
Hello Flomo

As Jesse suggested, StringBuilder is faster than Regex in this case becasue
the regular expression ^"|"$ needs to match the pattern over the whole
string.

Please feel free to let me know if you have any other concerns.

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 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.