问题
I want to replace string which is a square bracket with another number. I am using regex replace method.
Sample input:
This is [test] version.
Required output (replacing "[test]" with 1.0):
This is 1.0 version.
Right now regex is not replacing the special character. Below is the code which I have tried:
string input= "This is [test] version of application.";
string stringtoFind = string.Format(@"\b{0}\b", "[test]");
Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
There may be any special character in input and stringtoFind variables.
回答1:
You must account for two things here:
- Special characters must be escaped with a literal
\
symbol that is best done usingRegex.Escape
method when you have dynamic literal text passed as a variable to regex - It is not possible to rely on word boundaries,
\b
, because the meaning of this construct depends on the immediate context.
What you may do is use Regex.Escape
with unambiguous word boundaries (?<!\w)
and (?!\w)
:
string input= "This is [test] version of application.";
string key = "[test]";
string stringtoFind = $@"(?<!\w){Regex.Escape(key)}(?!\w)";
Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
Note that if you want to replace a key string when it is enclosed with whitespaces use
string stringtoFind = $@"(?<!\S){Regex.Escape(key)}(?!\S)";
^^^^^^ ^^^^^
回答2:
This seems to me to be the closest to exactly what you're asking for:
string input = "This is [test] version of application.";
string stringtoFind = Regex.Escape("[test]");
Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
That outputs This is 1.0 version of application.
.
However, in this case, simply doing this would suffice:
string input = "This is [test] version of application.";
Console.WriteLine(input.Replace("[test]", "1.0"));
It does the same thing.
回答3:
My guess is that this simple expression might likely work:
\[[^]]+\]
Test
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\[[^]]+\]";
string substitution = @"1.0";
string input = @"This is [test] version";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
The expression is explained on the top right panel of this demo, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.
回答4:
\] // Matches the ]
\[ // Matches the [
Here is a Cheat Sheet you can use in the future https://www.rexegg.com/regex-quickstart.html#morechars
string input = "This is [test] version of application.";
string stringtoFind = string.Format(@"\[[^]]+\]", "[test]");
Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
Console.ReadKey();
https://www.regexplanet.com/share/index.html?share=yyyyujzkvyr => Demo
回答5:
You should escape the brackets and remove \b
:
string input= "This is [test] version of application.";
string stringtoFind = string.Format("{0}", @"\[test\]");
Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
Output
This is 1.0 version of application.
Important note
\b
does NOT match spaces. \b
matches the empty string at the beginning or end of a word. Maybe you were looking for \s
.
来源:https://stackoverflow.com/questions/56964507/word-boundaries-not-matching-when-the-word-starts-or-ends-with-special-character