Regular Expression to Allow Backslash in C#

Detecting \ (backslash) using Regex

The \ is used even by Regex(es). Try "[\"\'\\\\/]+" (so double escape the \)

Note that you could have @"[""'\\/]+" and perhaps it would be more readable :-) (by using the @ the only character you have to escape is the ", by the use of a second "")

You don't really need the +, because in the end [...] means "one of", and it's enough for you.

Don't eat what you can't chew... Instead of regexes use

// result is true if no match
result = input.IndexOfAny(new[] { '"', '\'', '\\', '/' }) == -1;

I don't think anyone ever lost the work because he preferred IndexOf instead of a regex :-)

Regex to match backslash inside a string

Your regex can mean two things, depending on whether you are declaring it as a raw string or as a normal string.

Using:

"^[a-zA-Z_][\\\]?[a-zA-Z0-9_]+$"

Will not match any of your test examples, since this will match, in order:

  • ^ beginning of string,
  • [a-zA-Z_] 1 alpha character or underscore,
  • [\\\]? 1 optional backslash,
  • [a-zA-Z0-9_]+ at least 1 alphanumeric and/or underscore characters,
  • $ end of string

If you use it as a raw string (which is how regexhero interpreted it and indicated by the @ sign before the string starts) is:

@"^[a-zA-Z_][\\\]?[a-zA-Z0-9_]+$"
  • ^ beginning of string,
  • [a-zA-Z_] 1 alpha character or underscore,
  • [\\\]?[a-zA-Z0-9_]+ one or more characters being; backslash, ], ?, alphanumeric and underscore,
  • $ end of string.

So what you actually need is either:

"^[a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+$"

(Two pairs of backslashes become two literal backslashes, which will be interpreted by the regex engine as an escaped backslash; hence 1 literal backslash)

Or

@"^[a-zA-Z0-9_]+\\[a-zA-Z0-9_]+$"

(No backslash substitution performed, so the regex engine directly interprets the escaped backslash)

Note that I added the numbers in the first character class to allow it to match numbers like you requested and added the + quantifier to allow it to match more than one character before the backslash.

Regex with a backslash as a pattern terminator

Alternative assuming its a unc path:

string unc_server_name = new Uri(@"\\JJ01G3C5-10A6S0\C$\2015\Mar\24\").Host;

How do I escape a backslash in the following regex c#

A \ is a special escaping character in regular expressions. You have to escape it so that it will be interpreted as a literal \ and not an escape sequence. $ is also a special character (an end anchor), so you'll want to escape that as well.

string pathToAmmend = @"\$SERVERROOT\\pathpath";

Using @ to create a verbatim string only means you don't have to escape the \ for the sake of the C# compiler. You still have escape the \ for in a regular expression pattern. Without the verbatim string this would be:

string pathToAmmend = "\\$SERVERROOT\\\\pathpath";

Of course, as Jon Skeet points out, for something this simple, regular expressions aren't really the best way to go here.

How to allow only double backslash in email with Regex?

How it should be escaped depends a little on what language you are using, but here you go:

^(([-\w\/\.]|\\\\)+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$

See it in action

All I did is add a forward slash in the first group \/ and add two back slashes as an alternative |\\\\.


Here is the C# escaped version:

^(([-\\w\\/\\.]|\\\\\\\\)+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$

Dynamic Regex replaces single backslash with double backslash in C#

Finally I solved it.

What I am doing here is instead of using "Regex.IsMatch()" directly,I created a Regex object and passing the dynamic value from the Sql server database i.e. ^(+44\s?7\d{3}|(?07\d{3})?)\s?\d{3}\s?\d{3}$, then check whether the input matches.

Code:

      string sRegxE =dbContext.GetFields.Where(s => s.Name == sColumnName).Select(s => s.ExpressionValue).FirstOrDefault();
Regex RgxM = new Regex("" + sRegxE + "");
Match isPhoneNumber = RgxM.Match(sColumnValue);
if (isPhoneNumber.Success)
{
//Do somthing...
}

Reference:
Capture variable string in a regular expression?

Disallowing Backslashes in a Regular Expression C#

You need to do a double escape. Try this:

Regex regex = new Regex("TIM[1-9]|BIN[1-9]|[<>:\\\\\"/|?*]");

Explanation:

You need to escape the backslash in C# strings to get a backslash in the string. Additionally, the string needs to have two backslashes, because Regex also requires the backslashes to be escaped.

BTW, using verbatim strings makes it a bit more readable:

Regex regex = new Regex(@"TIM[1-9]|BIN[1-9]|[<>:\\""/|?*]");

Both codes will result in a Regex with this expression:

TIM[1-9]|BIN[1-9]|[<>:\\"/|?*]


Related Topics



Leave a reply



Submit