Maybe simple question..
String text = "fake 43 60 fake";
String patt = "[43.60]";
Match m = Regex.Match(text, patt)
In this situation, m.Success = true because the dot replace any character (also the space). But I must match the string literally in the patt.
Of course, I can use the '\' before the dot in the patt
String patt = #"[43\.60]";
So the m.Success = false, but there's more special characters in the Regular Expression-world.
My question is, how can I use regular expression that a string will be literally taken as it set. So '43.60' must be match with exactly '43.60'. '43?60' must be match with '43?60'....
thanks.
To get a regex-safe literal:
string escaped = Regex.Escape(input);
For example, to match the literal [43.60]:
string escaped = Regex.Escape(#"[43.60]");
gives the string with content: \[43\.60].
You can then use this escaped content to create a regex; for example:
string find = "43?60";
string escaped = Regex.Escape(find);
bool match = Regex.IsMatch("abc 43?60", escaped);
Note that in many cases you will want to combine the escaped string with some other regex fragment to make a complete pattern.
Related
i want to replace ab followed by var11 in given string
Input:|var11=ab|var12=100|var21=cd|var22=200|
My code is as follows:
string input = "|var11=ab|var12=100|var21=cd|var22=200|";
string pattern = #"^.var11=([a-z]+).";
string value = Regex.Replace(input, pattern, "ep");
and the output I got is:
epvar12=100|var21=cd|var22=200|
but the expected output was:
|var11=ep|var12=100|var21=cd|var22=200|
You may use
string input = "|var11=ab|var12=100|var21=cd|var22=200|";
string pattern = #"(?<=\bvar11=)[^|]+";
string value = Regex.Replace(input, pattern, "ep");
Or, a capturing group approach:
string pattern = #"\b(var11=)[^|]+";
string value = Regex.Replace(input, pattern, "${1}ep");
See the .NET regex demo
Details
(?<=\bvar11=) - a location immediately preceded with a whole word var11=
[^|]+ - 1+ non-pipe chars.
If you want to update the var11 value only when it is preceded with | or at the start of string use
string pattern = #"(?<=(?:^|\|)var11=)[^|]+";
where (?:^|\|) matches start of string (^) or (|) a pipe char (\|).
I want to replace a string if it is a part of another string from both ends.
Say for example a string +35343+3566. I want to replace +35 with 0 only if it is surrounded with characters from both sides. So desired outcome would be +35343066.
Normally I'd use line.Replace("+35", "0") and perhaps if-else to meet a condition
string a = "+35343+3566";
string b = a.Replace("+35", "0");
I would want 'b = +35343066 and not b = 0343066`
You can use regex for this. For example:
var replaced = Regex.Replace("+35343+3566", "(?<=.)(\\+35)(?=.)", "0");
// replaced will contain +35343066
So what this pattern is saying is that +35 (\\+35) must have one character behind (?<=.) and one character ahead (?=.)
You can do this with a Regular Expression, as follows:
string a = "+35343+3566";
var regex = new Regex(#"(.)\+35(.)"); // look for "+35" between any 2 characters, while remembering the characters that were found in ${1} and ${2}
string b = regex.Replace(a, "${1}0${2}"); // replace all occurences with "0" surrounded by both characters that were found
See Fiddle: https://dotnetfiddle.net/OdCKsy
Or slightly simpler, if it turns out that only the prefix character matters:
string a = "+35343+3566";
var regex = new Regex(#"(.)\+35"); // look for a character followed by "+35", while remembering the character that was found in ${1}
string b = regex.Replace(a, "${1}0"); // replace all occurences with the character that was found followed by a 0
See Fiddle: https://dotnetfiddle.net/9jEHMN
I have a regex string:
string regex =
"\"\\d*\",\"(?<url>\\w|\\d|[().,-–_'])\".*";
And a string I want to match it against:
string line =
"\"4\",\"1800_in_sports\",\"24987709\",\"\",\"1906\",\"20171028152258\"";
When I try to get the url category, or even check for a match, there is no match:
var result = Regex.Match(line, regex);
string output = result.Groups["url"].Value;
If i try Regex.IsMatch(..) it also returns false.
I used http://regexstorm.net/tester to test this and it works there, but, not when I run the code.
In RegexStorm I used the pattern:
"\d{1,3}","(?<url>\w|\d|\n|[().,-–_'])+?"
Replace \\d with just \d and \\w with just \w.
As Dour High Arch mentioned, verbatim string should be used. Adding double quotes in front of double quotes allows for verbatim strings.
Changing string regex to:
string regex =
#"""\d{1,3}"",""(?<url>\w|\d|\n|[().,-–_''])+?""";
Now returns a match.
I'm doing some experiments with regular expressions and I don't know why the regex don't match.
string line is one line from a file. A line which should match is this
["boxusers:settings/user[boxuser11]/name"] = "username",
The number of the boxuser and the value could be different, so I tried to find a regular expression
My code is this:
string user;
string patternUser = "[\"boxusers:settings/user[boxuser\\d{2,}]/name\"] = \"";
if (Regex.Match(line,patternUser).Success)
user = Regex.Replace(Regex.Replace(line, patternUser, String.Empty), ",*", String.Empty);
So I think \d{2,0} should be a number with two digits and the rest is just the same. But the regex just don't match.
What's going wrong?
Square brackets have a special significance in regular expressions. You need to escape them with a backslash.
var line = #"[""boxusers:settings/user[boxuser11]/name""] = ""username"", ";
string patternUser = #"\[""boxusers:settings/user\[boxuser\d{2,}\]/name""\] = """;
Console.WriteLine(Regex.Match(line, patternUser).Success);
If you don't want to use verbatim strings, you'll need to use two backslashes to escape each regex metacharacter (the first to escape the second).
I have the following string: aWesdE, which I want to convert to http://myserver.com/aWesdE.jpg using Regex.Replace(string, string, string, RegexOptions)
Currently, I use this code:
string input = "aWesdE";
string match = "(.*)";
string replacement = "http://myserver.com/$1.jpg";
string output = Regex.Replace(input, match, replacement,
RegexOptions.IgnoreCase | RegexOptions.Singleline);
The result is that output is ending up as: http://myserver.com/aWesdE.jpghttp://myserver.com/.jpg
So, the replacement value shows up correctly, and then appears to be appended again - very strange. What's going on here?
There are actually 2 matches in your Regex. You defined your match like this:
string match = "(.*)";
It means match zero or more characters, so you have 2 matches - empty string and your text. In order to fix it change the pattern to
string match = "(.+)";
It means match one or more characters - in that case you will only get a single match