Simple Regex Question

  • Thread starter Thread starter bg_ie
  • Start date Start date
B

bg_ie

I have the following Regex -

oldString = @"C:\ta";
Regex regexEndsInBackslash = new Regex(oldString + @"\\",
RegexOptions.Singleline | RegexOptions.IgnoreCase);

Any ideas why none of the following are true -

if (regexEndsInBackslash.Match(@"C:\ta\").Success == true)
....
if (regexEndsInBackslash.Match(@"C:\\ta\\").Success == true)
....

Thanks,

Barry.
 
Hi Barry,

in regular expressions '\t' is the escapesequence for a tab character. If
you want to find '\t' you've got to escape the '\' itself by '\\'.

try
oldString = @"C:\\ta";

likewise @"\\" in your code gives the regex-representation for a single '\'.

First you have to think, wich charactersequence your regex must have, what
may need escapesesequences. Then you've got to think, how this character
sequences is to be represented as literal in C# (or wich ever language you
use).

hth
Christof
 
Hi Barry,

in regular expressions '\t' is the escapesequence for a tab character. If
you want to find '\t' you've got to escape the '\' itself by '\\'.

try
oldString = @"C:\\ta";

likewise @"\\" in your code gives the regex-representation for a single '\'.

First you have to think, wich charactersequence your regex must have, what
may need escapesesequences. Then you've got to think, how this character
sequences is to be represented as literal in C# (or wich ever language you
use).

hth
Christof

Ah, I see, thanks for your help. This works -

string oldString = @"C:\\ta";
Regex regexEndsInBackslash = new Regex(oldString ,
RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (regexEndsInBackslash.Match(@"C:\ta").Success == true)
...

But I use oldString = @"C:\ta"; throughout my program. How can I
escape the backslashes in oldString when using it in my
regexEndsInBackslash definition? Use another Regex?

Thanks again,

Barry.
 
Ah, I see, thanks for your help. This works -
string oldString = @"C:\\ta";
Regex regexEndsInBackslash = new Regex(oldString ,
RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (regexEndsInBackslash.Match(@"C:\ta").Success == true)
...

But I use oldString = @"C:\ta"; throughout my program. How can I
escape the backslashes in oldString when using it in my
regexEndsInBackslash definition? Use another Regex?

Use RegEx.Eccape. It converts a string to its representation in RegEx.

Christof
 
Hi Barry,

in regular expressions '\t' is the escapesequence for a tab character. If
you want to find '\t' you've got to escape the '\' itself by '\\'.

try
oldString = @"C:\\ta";

likewise @"\\" in your code gives the regex-representation for a single '\'.

First you have to think, wich charactersequence your regex must have, what
may need escapesesequences. Then you've got to think, how this character
sequences is to be represented as literal in C# (or wich ever language you
use).

hth
Christof

This works, but I think I'd be forgiven for finding it just a little
strange -

oldString = Regex.Replace(oldString, @"\\", @"\\");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top