This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.
Related
This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 6 years ago.
Issue
I am having an issue creating a regex to accept any string and the ENTER key, at the moment i have this:
^$|^.+$
I have looked around and people have said to add \n but this does not work.
An example of the string is should allow is as follows:
Hello this is a test string
and i want this to be accepted
Try setting the s flag on the regex engine. This will ensure that the . metacharacter will match newlines.
Here's a link to a working example.
Also, as a sidenote, instead of ^$|^.+$ you can condense the whole expression to ^.*$ to achieve the same results with better performance.
In C#, you need the RegexOptions.Singleline option. See this SO post for more information.
Here is a quick example that really just matches the entire string, so it's not useful.
var regex = new Regex(#"^.*$",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
In your future validation code, you need to replace .* with whatever your validation will be.
This question already has answers here:
Regex to pick characters outside of pair of quotes
(6 answers)
Closed 7 years ago.
I am new in writing regular expression and I have the following Scenario.
I have a string, like :
string line = "if (true){var data = string.Format(\"something {0} {1}.\", \"is\", \"wrong\");}";
now I need to write a regular expression that just pick the closing curly braces which are not in the double quote
so far I tried this:
"(^(\"[^\"]*\")(}))+"
^(\"[^\"]*\") : I want to Ignore any substring which is inside double quote, AND
(}) : I want to take }
+: for at least 1 occurrence.
But it seems I Did something wrong. Could any one please guide me to sort out where I did the wrong?
Thank you.
You just need these parts of your regex:
(?:\"[^\"]*\")|(})
Regex live here.
This question already has answers here:
How to use a string with quotation marks inside it?
(7 answers)
Closed 7 years ago.
I'm trying to make my program press the " (Shift + 2 in the UK) when a button is pressed, but I'm not sure how to as when I enter..
SendKeys.Send(""");
It converts the latter part of the code to string.
Is there a way to make the program ignore a particular character? I'm not really sure how to go about this.
I'm making an on screen keyboard if anyone is wondering.
Escape the ", like so:
SendKeys.Send("\"");
Or use a string literal:
SendKeys.Send(#"""");
String literals in C# are usually nicer to read, but in this case, you still need to escape the single quote in the string literal, by doubling it.
In fact this is an interesting example of the one approach vs. the other:
"\"" // 4 chars, not very readable
vs:
#""""" // 5 chars: even LESS readable: pretty confusing what it means
SendKeys.Send("\"");
Something like this should work.
Have a look at this (what you're looking for is called escaping characters)
You can use escape characters:
SendKeys.Send("\"");
The \" translates to just ".
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.