This question already has answers here:
Can I expand a string that contains C# literal expressions at runtime
(5 answers)
Closed 3 years ago.
I'm implementing a compiler, and I need to convert from an escape character literal written in source code file to an actual value.
For example I might have a line of source code char = '\\'. I then parse that and get given the string "'\\'", which I need to turn into the actual char '\\'.
char.Parse and char.TryParse both fail when parsing a char in escape sequence form. For example:
char.Parse(#"\\");
Will throw "String must be exactly one character long."
Is there any way to parse everything on this list as a char (ignoring those that are to big to fit in a UTF16 char).
the # makes it a verbatim string literal. drop that and \ will be treated as the escape char for the next \.
#"\\".Length // 2
"\\".Length // 1
Related
This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I am having this below regex pattern. I want to allow these below characters as special characters.
{
}
[
]
Regex regEx = new Regex(#"^(?=.*[A-Z])(?=.*[!##$%^&*()+_<>~-])(?=.*[0-9])(?=.*[a-z]).{8,15}$");
Is there a way to add them
You'll need to escape them within the regex. Within a regular expression, you can refer to "\[" to get a '[' character, for example.
Note that in C, C#, C++, Java, etc, you usually need to double up the '\' character to escape the escape character. So when adding '[' to your regex, you would actually use "\\[". In your case you're using an #"" so you've escaped that trap.
This question already has answers here:
Regular expression to match numbers with or without commas and decimals in text
(11 answers)
Closed 5 years ago.
How do I restrict special characters and character in a Textbox?
I'm using this code but I am not restricting special characters and character
code :-
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, #"^\\d*\\.?\\d*$"))
{
// Write Code
}
if you're using # in front of string, you don't have to escape characters. So, remove all those extra backslash chars and your regex should work. Like this:
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, #"^\d*\.?\d*$"))
EDIT:
use sites like this to test your regex.
This question already has answers here:
Unrecognized escape sequence for path string containing backslashes
(5 answers)
Closed 7 years ago.
I'm trying to validate a password field using regex under the namespace System.Text.RegularExpressions but I'm getting three errors for
'Unrecognized escape sequence'.
When I double click on the errors it highlights the '-' in my expression for the character range but I don't know why this is wrong.
// password must contain one uppercase, one lowercase and one digit
Regex reg = new Regex("^(?=.*[!##$%^&*()\-_=+`~\[\]{}?|])(?=.+[a-z])(?=.+[A-Z])(? =.+[0-9]).{8,50}$");
Just add an # before the first quote to make it a verbatim string literal or escape the backslashes as \\.
it seems you have one space after ?
(? =.+[0-9]).{8,50}
remove that.
This question already has answers here:
What is the difference between a regular string and a verbatim string?
(6 answers)
Closed 9 years ago.
From ReSharper, I know that
var v = #"something";
makes v something called a verbatim string. What is this and what is a common scenario to use it?
In a verbatim string, escape sequences (such as "\n" for newline) will be ignored. This helps you type strings containing backslashes.
The string is also allowed to extend over multiple lines, for example:
var s = #"
line1
line2";
The string will appear the same way you typed it in your source code, with line breaks, so you don't have to worry about indents, newlines etc.
To use quotes inside a verbatim literal, you just double them:
#"This is a string with ""quotes""."
It means that special chars don't need to be escaped, since you informed the compiler to expect special characters, and to ignore them. A common use case might be to specify a connection string:
string sqlServer = #"SERVER01\SQL";
This is perfectly valid, as opposed to in normal use where the backslash would be considered an escape character.