473,695 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1944
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.Collecti ons.Generic;
using System.Text;
using System.Text.Reg ularExpressions ;
using System.Diagnost ics;
using System.Collecti ons;

namespace ConsoleApplicat ion1
{
public delegate string TestStringDeleg ate(string input);

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

static Program()
{
replaceBoth.Mat ch("");
replaceLeft.Mat ch("");
replaceRight.Ma tch("");
}

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

foreach (string input in inputs)
{
Test("substring ", input, new TestStringDeleg ate(TestSubstri ng));
Test("stringbui lder", input, new TestStringDeleg ate(TestStringB uilder));
Test("regexboth ", input, new TestStringDeleg ate(TestRegexBo th));
Test("regextwop art", input, new TestStringDeleg ate(TestRegexTw oPart));
}

WriteResults();
Console.ReadLin e();
}

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

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

if (results.Contai nsKey(name))
{
long millis = results[name];
results[name] = millis + sw.ElapsedMilli seconds;
}
else
{
results.Add(nam e, sw.ElapsedMilli seconds);
}
Console.WriteLi ne(input);
Console.WriteLi ne(output);
Console.WriteLi ne(sw.ElapsedMi lliseconds);
Console.WriteLi ne("End test " + name);
}

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

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

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

static string TestRegexBoth(s tring input)
{
return replaceBoth.Rep lace(input, "");
}

static string TestRegexTwoPar t(string input)
{
return replaceRight.Re place(replaceLe ft.Replace(inpu t, ""), "");
}

private static Regex replaceBoth = new Regex(@"^""|""$ ", RegexOptions.Co mpiled);
private static Regex replaceLeft = new Regex(@"^""", RegexOptions.Co mpiled);
private static Regex replaceRight = new Regex(@"""$", RegexOptions.Co mpiled);
}
}
--
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.Collecti ons.Generic;
using System.Text;
using System.Text.Reg ularExpressions ;
using System.Diagnost ics;
using System.Collecti ons;

namespace ConsoleApplicat ion1
{
public delegate string TestStringDeleg ate(string input);

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

static Program()
{
replaceBoth.Mat ch("");
replaceLeft.Mat ch("");
replaceRight.Ma tch("");
}

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

@"""........... ............... ............... ............... ............... .....""",
@"""........... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ........""",
@"""........... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ....
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... ............... ........
............... ............... ............... ............... ............... .... """
}; foreach (string input in inputs) {
Test("substring ", input, new TestStringDeleg ate(TestSubstri ng));
Test("stringbui lder", input, new TestStringDeleg ate(TestStringB uilder));
Test("regexboth ", input, new TestStringDeleg ate(TestRegexBo th));
Test("regextwop art", input, new TestStringDeleg ate(TestRegexTw oPart)); }

WriteResults();
Console.ReadLin e();
}

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

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

if (results.Contai nsKey(name))
{
long millis = results[name];
results[name] = millis + sw.ElapsedMilli seconds;
}
else
{
results.Add(nam e, sw.ElapsedMilli seconds);
}
Console.WriteLi ne(input);
Console.WriteLi ne(output);
Console.WriteLi ne(sw.ElapsedMi lliseconds);
Console.WriteLi ne("End test " + name);
}

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

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

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

static string TestRegexBoth(s tring input)
{
return replaceBoth.Rep lace(input, "");
}

static string TestRegexTwoPar t(string input)
{
return replaceRight.Re place(replaceLe ft.Replace(inpu t, ""), "");
}

private static Regex replaceBoth = new Regex(@"^""|""$ ", RegexOptions.Co mpiled);
private static Regex replaceLeft = new Regex(@"^""", RegexOptions.Co mpiled);
private static Regex replaceRight = new Regex(@"""$", RegexOptions.Co mpiled);
}
}
--
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
2082
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 http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
3
2335
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 commas allowed, but may contain parenthesis which I would like to keep apart to drop later. They may be separated by comas, spaces or semicolon.
7
10219
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 ignore pairs of brackets like: "one" <tab> "two three" ( four "five six" ) Want fields like:
6
4791
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 string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
3
2135
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 someone puts something in quotes i.e "A Gibbon" i want to extract that prior to using the rest of the string what i need is a regex string that'll turn a postback string eg "A Gibbon" "A Baboon" "George Bush" doris day into a csv array
5
1770
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 I can't figure out how to escape a quote. The closest I got was Regex.split(String,"(\"")"), which gives me an array of strings: "X:Y\Z.exe
4
3204
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 having a heck of a time figuring out a regex for it. Can anyone assist with a regex to find all the AND's not in double quotes? An example sentence might be: red and blue and "crazy elephant" and "orange and red" and stuff.
8
2587
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: @"@?""""|@?"".*?(?!\\).""|''|'.*?(?!\\).'" I am inputting the entire source file string and using it with RegexOptions.Singleline. This works OK with, unless the string ends with a back-slash. For example: "This is a test\\". Can anybody see how to fix this...
6
2563
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 catch standard " sign and non-standard “ and ” - but non-standard double quotes are not recognised by Replace function. Can anyone suggest the correct expression to use (I was trying to avoid multiple String.Replace functions that work fine - I...
9
8872
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 and commas, and can use quotes to encapsulate multiple words. An example: tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6" So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4...
0
8586
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2289
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.