how to match parentheses with regex in c# [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression to match outer brackets
I'm trying to match a string that contains parentheses but escaping the parentheses with a backslash gives me an error "unrecognized escape sequence". How do i match the parenthesis and retrieve whats inside?

use Regex.Escape(): http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape.aspx

Related

How to get all possible Regex Matches [duplicate]

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

Use RegEx to find 100$ in text c# [duplicate]

This question already has answers here:
C# Regex Match whole word, with special characters
(2 answers)
Regex whitespace word boundary
(3 answers)
Closed 4 years ago.
I am trying to search 100$ through the RegEx. But unable to find its pattern is as above.
\b100\$\b
However, if the text contains 100$1 then it displays properly.
But I want to do exact search.
Word boundaries only work with word characters, not $. Try using this regex:
\b100\$(?=\s|\$)
This will match 100$, where what follows the $ is either whitespace or the end of the line.
Demo

Unrecognized escape sequence \_. C# regex [duplicate]

This question already has an answer here:
C# Regex, Unrecognized escape sequence
(1 answer)
Closed 5 years ago.
I have the following regex which is working ok in https://regex101.com/ and so I want to use it in C# but I get the error Unrecognized escape sequence \_.
^https:\/\/github\.com\/([a-zA-Z\-0-9]+)\/([a-zA-Z\-\_]+)\/commit\/([a-fA-F0-9]{40}),(.*),([0-9]+),([0-9]+)$
in C# I do that:
string regex = #"^https:\/\/github\.com\/([a-zA-Z\-0-9]+)\/([a-zA-Z\-\_]+)\/commit\/([a-fA-F0-9]{40}),(.*),([0-9]+),([0-9]+)$";
and then want to do:
if (Regex.IsMatch(input, regex)) and so on. the error is here on that line.
I cant understand why I get the error in case I use #
You don't need to escape underscores in C# regular expressions. The error is coming from the regular expression parser.
You mentioned it is working OK on regex101.com. Maybe double check that it is dealing with underscores in the way you expect.

C# .NET Regex 'Unrecognized escape sequence' [duplicate]

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.

how to escape multiple curly brackets in C#? [duplicate]

This question already has answers here:
How to escape braces (curly brackets) in a format string in .NET
(11 answers)
Closed 9 years ago.
I need to escape the first pair of curly brackets but the second pair or the inner pair should not be escaped.
string.Format("#Neptune.ShowAlert(\{content:{0}\})", ex.Message);
I have tried the above but I get the "Unrecognized escape sequence" error.
Use {{ and }} to escape the braces.

Categories