473,386 Members | 1,758 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,386 software developers and data experts.

Simple Regex Question

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.

Apr 25 '07 #1
4 1478
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
Apr 25 '07 #2
On 25 Apr, 17:49, "Christof Nordiek" <c...@nospam.dewrote:
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.

Apr 26 '07 #3
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
Apr 26 '07 #4
On 25 Apr, 17:49, "Christof Nordiek" <c...@nospam.dewrote:
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, @"\\", @"\\");

Apr 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me...
2
by: K. Shier | last post by:
my regex for matching phone #'s: \({0,1}(?<areacode>{3})\){0,1}( |-){0,1}(?<prefix>{3})( |-){0,1}(?<suffix>{4}) which i then .Replace to become: (${areacode}) ${prefix}-${suffix} question: ...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
20
by: Larry Woods | last post by:
I'm drawing a blank... What is the regular expression for search a string for NO occurances of a substring? Example: I want to find all lines that do NOT have the substing "image" in them. ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
2
by: Warren | last post by:
Sorry to ask something so elementary, but I'm just not getting it. suppose I want to match the value of parameter b from a URL like http://www.abcd.com/?a=1&b=2&c=3 I can use this regex...
4
by: MaxMax | last post by:
I want to add a text to another text using regular expressions... An example before= "foo" after= "foobar" before= "one" after= "onebar" before = "" after = "bar"
2
by: Sin Jeong-hun | last post by:
I'm trying to replace \n with @(representing the actual new line character). Of course, \\n should not be replaced. What I'm trying to do is simply what the good-old printf does. For example, "\\n...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.