Regexp and &

  • Thread starter Thread starter Jimmy
  • Start date Start date
J

Jimmy

Hello,

If I want to check in C# code that if there is a & in my string variable
using
RegExp, how should I inform RegExp about &-char? Is just &, \&, & or
something else? I have strucled with this small issue two days and I still
not
sure how C# accpets &-char in Regular Expresions.

Can anyone help?

Cheers!
 
Jimmy said:
If I want to check in C# code that if there is a & in my string variable
using
RegExp, how should I inform RegExp about &-char? Is just &, \&, & or
something else? I have strucled with this small issue two days and I still
not
sure how C# accpets &-char in Regular Expresions.

Just out of interest, is there any reason you need to use a regular
expression at all? Using String.IndexOf - or even better,
String.Contains if you're using .NET 2.0 - is simpler.

However, if you really need to use regexes for some reason, you
shouldn't need anything special. For example:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
Regex re = new Regex("&");
Console.WriteLine (re.IsMatch("here & there"));
Console.WriteLine (re.IsMatch("here and there"));
}
}

prints true then false, as expected.

Jon
 
Yes, I need RegExps in this project.

But as your sample shows & can be without \ or other characters.

Thanks!
 
Treat it just like any other character in your Regular Expression.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
No I noticed a thing that I do not understand.
In C# RegExp like this
Regex re = new Regex("^[0-9&]{1}$");

System.Diagnostics.Debug.WriteLine(re.IsMatch("here&there"));

System.Diagnostics.Debug.WriteLine(re.IsMatch("hereandthere"));

will produce false and false

But in RegEx Coach tool it's okay.

So, Can anyone explain what is the difference with result in C# and
RegEx Coach?
 
Jimmy said:
No I noticed a thing that I do not understand.
In C# RegExp like this
Regex re = new Regex("^[0-9&]{1}$");

System.Diagnostics.Debug.WriteLine(re.IsMatch("here&there"));

System.Diagnostics.Debug.WriteLine(re.IsMatch("hereandthere"));

will produce false and false

But in RegEx Coach tool it's okay.

So, Can anyone explain what is the difference with result in C# and
RegEx Coach?

Well, I don't know anything about RegexCoatch, but your expression
matches exactly 1 character out of 0-9&, and only if that character is
at the start and end of the string.

(Note that {1} doesn't really do anything - it's an "exactly one"
quantifier, which is the default unless I've missed something.)

The ^ means "start of the string" and "$" means "end of the string".
 
I was wondering that maybe my problem is in the XMLReader which
reads RegExps to memory.

Every time I read &-character in config file that has RegExps, XMLTextReader
throws an Exception. Code is like below, could this be the real problem ...

XmlDocument oXml = new XmlDocument();
XmlTextReader reader = new XmlTextReader(ConfigFile);
reader.MoveToContent();
reader.Read();
oXml.Load(reader); // This throws an Exception if RegExp has &
character!!!!!!
XmlNodeList oList = oXml.GetElementsByTagName("appSettings");
AppSettings = new NameValueCollection();

foreach(XmlNode oNode in oList)
{
foreach(XmlNode oKey in oNode.ChildNodes)
{
AppSettings.Add(oKey.Attributes["key"].Value,
oKey.Attributes["value"].Value);
}
}

reader.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}








Jon Skeet said:
Jimmy said:
No I noticed a thing that I do not understand.
In C# RegExp like this
Regex re = new Regex("^[0-9&]{1}$");

System.Diagnostics.Debug.WriteLine(re.IsMatch("here&there"));

System.Diagnostics.Debug.WriteLine(re.IsMatch("hereandthere"));

will produce false and false

But in RegEx Coach tool it's okay.

So, Can anyone explain what is the difference with result in C# and
RegEx Coach?

Well, I don't know anything about RegexCoatch, but your expression
matches exactly 1 character out of 0-9&, and only if that character is
at the start and end of the string.

(Note that {1} doesn't really do anything - it's an "exactly one"
quantifier, which is the default unless I've missed something.)

The ^ means "start of the string" and "$" means "end of the string".
 
Jimmy said:
I was wondering that maybe my problem is in the XMLReader which
reads RegExps to memory.

Every time I read &-character in config file that has RegExps, XMLTextReader
throws an Exception. Code is like below, could this be the real problem ...

Absolutely - you need to escape ampersands in XML. This has nothing to
do with regular expressions - it's just the way XML works. Try using
& instead, and when you've loaded the value, print it out to the
console so you can check it. That way you can separate any potential
issues with the regular expression from issues in reading it in from
the XML.
 
Back
Top