如何使用Regex。替换为替换两个点?

时间:2022-02-05 16:47:58

I need to find in a string a pattern with two dots (..) e.g.:

我需要在字符串中找到一个有两个点的图案。

Example 1:

示例1:

axdb..TXU 

and replace it with

和替换它

TXU@axdb_LNK

another example would be e.g.:

另一个例子是:

Example 2

示例2

ssrrdb..WOPXLP

and replace it with

和替换它

WOPXLP@ssrrdb_LNK

It could occur once or many times in the string and there could be any number of letters before or after the double dots. Also, there will be other text in the string. e.g:

它可能在字符串中出现一次或多次,也可能在双点之前或之后出现任意数量的字母。此外,字符串中还有其他文本。例句:

SELECT col2 FROM axdb..TXU a WHERE a.col1 = 1 
(could also be select * from axdb..TXU )

would be changed to

将被改变

SELECT col2 FROM TXU@axdb_LNK a WHERE a.col1 = 1 
(could also be select * from TXU@axdb_LNK)

2 个解决方案

#1


1  

Try this regex:

试试这个正则表达式:

(\S+)\.\.(\S+)

Description

如何使用Regex。替换为替换两个点?

Sample code

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

#2


2  

UPDATED:Consider the following code snippet...

更新:考虑以下代码片段…

string inputMessage = @"SELECT col2 FROM axdb..TXU a WHERE a.col1 = 1 
(could also be select * from axdb..TXU )";
var match = Regex.Match(inputMessage, @"(?<1>\w*)\.\.(?<2>\w*)");
string outputMessage = inputMessage.Replace(match.Value, string.Format("{2}@{1}_LNK", match.Groups[0].Value, match.Groups[1].Value));

Good Luck!

好运!

#1


1  

Try this regex:

试试这个正则表达式:

(\S+)\.\.(\S+)

Description

如何使用Regex。替换为替换两个点?

Sample code

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

#2


2  

UPDATED:Consider the following code snippet...

更新:考虑以下代码片段…

string inputMessage = @"SELECT col2 FROM axdb..TXU a WHERE a.col1 = 1 
(could also be select * from axdb..TXU )";
var match = Regex.Match(inputMessage, @"(?<1>\w*)\.\.(?<2>\w*)");
string outputMessage = inputMessage.Replace(match.Value, string.Format("{2}@{1}_LNK", match.Groups[0].Value, match.Groups[1].Value));

Good Luck!

好运!