This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How to make a regex match case insensitive?
(1 answer)
Closed 5 years ago.
I am trying to do a find replace in a string of text. I am using Regex like this:
Regex regexText = new Regex("Test.Value");
strText = regexText.Replace(strText, value);
In this example I am trying to find the string "Test.Value" in a text string. However if this value appears in the string the replace does not happen.
If I remove the dots eg:
Regex regexText = new Regex("TEST");
strText = regexText.Replace(strText, value);
If I put the word "TEST" in the string, it replaces it just fine.
Is there a way to get this to work with strings with "."'s in?
You have to escape the dot:
Regex regexText = new Regex(#"Test\.Value");
As you wrote it, the regex is just looking for "Test", followed by any character except a line feed, followed by "Value".
On the top of that, if the text you are looking for is a little bit different, a case insensitive matching could help you out:
Regex regexText = new Regex(#"Test\.Value", RegexOptions.IgnoreCase);
Anyway, in this case I don't think a Regex is necessary. A simple string replace should do the job:
strText.Replace("Test.Value", value);
Related
This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 2 years ago.
I tried to use regexes in C#
^(?=.*\d)(?=.*[a - z])(?=.*[A - Z])(?=.*[!##$%^*])(?=.*[a-zA-Z]).{6,20}$
but \d comes as an error if i put [0-9] instead it wont work as desired
This should check the string has a uppercase, lowercase, symbol and a number
You should use [0-9]. Probably it is more correct... \d will catch non-european digits like рен (it is a Devanagari digit).
For the reason:
you probably wrote:
var rx = new Regex("\d");
But in this way the \d is an escape sequence of the string instead of being a regex.
Write
var rx = new Regex(#"\d");
to deactivate the escape expansion of strings.
This question already has answers here:
C# Regex Issue "unrecognized escape sequence"
(3 answers)
Regular expression to allow backslash in C#
(3 answers)
Closed 3 years ago.
I am looking to create a list of file inside a folder, all matching a regex.
But the regex crash whenever I put a path containing backslash inside the regex.
string test = #"C:\TEMP"; // or "C:\\TEMP"
Regex reg = new Regex(test + "\\" + "tstNL02" + #"_(.*).csv"); // it crash here
FileList = Directory.GetFiles(test).Where(path => reg.IsMatch(path)).ToList();
ArgumentException: parsing "C:\TEMP\tstNL02_(.*).csv" - Unrecognized escape sequence \T.
As far as I know, using a # or escaping the backslash should prevent the regex from interpreting backslashes in a string as an escaping character (and if I remove test from the regex but leave the \\, regex doesn't crash).
If I put #"C:\\TEMP" the Regex doesn't parse any and the match fail C:\\TEMP\tstNL02_(.*).csv
I fixed my problem by going another way but I was wondering why and how to fix this backslash-in-a-variable thing ?
Edit: problem didn't came from regex but from the fact I was using the same string for regex and Directory.GetFiles. Adding escaping backslashes to the string so Regex worked correctly would cause Directory.GetFiles to not escape those added backslashes, thus not matching files
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:
Regex Match all characters between two strings
(16 answers)
Closed 5 years ago.
I'm trying to parse a text looking for data inside this pattern:
{{([^]+)}}
i.e. any sequence of characters between {{ and }} .
But, when I try to build a Regex object:
Regex _regex = new Regex("{{([^]+)}}", RegexOptions.Compiled);
I got this error:
analysis of "{{([^]+)}}" - Set of [] not terminated....
whatever it means...
Someone has an hint?
The purpose of [^...] is to negate character classes present in the specified list. After the ^ symbol, in order to define a correct regular expression, you should include a set of characters to exclude like, for example [^a]+ (this matches one or more characters that don't include the literal a).
The regex you are attempting to define is probably:
{{\s*([\w]+)\s*}}
Visit this link for trying a working demo.
This is because [^] is not a valid regex, because you need to specify at least one symbol that you wish to exclude.
In order to capture the string up to the closing }} change the expression to this:
{{((?:[^}]|}[^}])*)}}
Demo.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I have run into a problem when trying to find multiple matches in a length of text. The text has the following format:
string text = "#IDENTIFIER http://www.link1.com #IDENTIFIER http://www.link2.org #IDENTIFIER http://www.link3.com #IDENTIFIER http://www.link4.net";
My objective is to extract each #IDENTIFIER link occurrence from this and I am doing it using the following code:
string pat = #"(#IDENTIFIER)(.*)\.(com|org|net)";
MatchCollection matches = Regex.Matches(text, pat);
foreach(Match match in matches) {
Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index);
}
The problem is, it returns one match and not 4. Why are the in-between patterns ignored?
Do you know what I am missing?
It's because .* on its own is generally greedy. Instead try using .*?:
string pat = #"(#IDENTIFIER)(.*?)\.(com|org|net)";