c# - 如何使用 Regex.Replace 替换两个点?

标签 c# regex

我需要在字符串中找到带有两个点 (..) 的模式,例如:

示例 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+)

描述

Regular expression visualization

示例代码

///<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/

相关文章:

c# - 在 BookControl 中显示 PDF。将 PDF 转换为 XAML?

c# - 在配置文件中使用引号作为值

regex - 如何提取ansible变量中的最后一个数字

regex - 如何在Elasticsearch中对动态定义的正则表达式进行有效搜索?

regex - 有什么作用? : in a regular expression mean?

c# - 检测页面刷新 jQuery 或 C# ASP.Net

c# - 如何在 LINQ 创建的字符串之前插入一个字符串?

c# - 如何计算字典中不包括第一项值的所有值的总和?

python - 用于匹配 URL 和失败的非 URL 的正则表达式

python - bvCase Insensitive Regex Replacement 来自字典