我需要在字符串中找到带有两个点 (..) 的模式,例如:
示例 1:
axdb..TXU
并将其替换为
TXU@axdb_LNK
另一个例子是:
示例 2
ssrrdb..WOPXLP
并将其替换为
WOPXLP@ssrrdb_LNK
它可以在字符串中出现一次或多次,双点之前或之后可以有任意数量的字母。此外,字符串中还会有其他文本。例如:
SELECT col2 FROM axdb..TXU a WHERE a.col1 = 1
(could also be select * from axdb..TXU )
会变成
SELECT col2 FROM TXU@axdb_LNK a WHERE a.col1 = 1
(could also be select * from TXU@axdb_LNK)
最佳答案
试试这个正则表达式:
(\S+)\.\.(\S+)
描述
示例代码
///<summary>
///
/// [1]: A numbered capture group. [\S+]
/// Anything other than whitespace, one or more repetitions
/// \.\.
/// Literal .
/// Literal .
/// [2]: A numbered capture group. [\S+]
/// Anything other than whitespace, one or more repetitions
///
///
///
/// </summary>
public Regex MyRegex = new Regex(
"(\\S+)\\.\\.(\\S+)",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
// This is the replacement string
public string MyRegexReplace = "$2@$1_LNK";
//// Replace the matched text in the InputText using the replacement pattern
string result = MyRegex.Replace(InputText,MyRegexReplace);
关于c# - 如何使用 Regex.Replace 替换两个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363467/