473,326 Members | 2,126 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,326 software developers and data experts.

Regex with quotes

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
4 1922
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
3
by: Craig Kenisston | last post by:
I have the sudden need to split a text that may have any of the following tokens : Words with quotes or double quotes. Words with no quotes at all. Numbers with and without decimal points, no...
7
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
3
by: Luis Esteban Valencia | last post by:
hello quite a simple one if you understand regular expressions vbscript and ..net, probably quite hard if you don't i have a single line input which offers classic search functionality, so if...
5
by: lgbjr | last post by:
Hello All, I have the following type of string: "X:Y\Z.exe" "123" What I need is an array of strings with the information from within each set of quotes. I was trying to use a Regex.Split, but...
4
by: JS | last post by:
I am writing a C# app that needs to parse a sentence entered by the user for a simple boolean search. I need to capture all of the AND words that are not inside of double quotes. However, I am...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
6
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to replace double quotes inside the text from the database with " to correctly display my text. I was trying to use Regex to perform such a task: Regex.Replace(text, "", """) to...
9
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.