如何在。net中只替换字符串中的第一个事件?

时间:2022-02-15 16:51:20

It seems the .NET Regex.Replace method automatically replaces all matching occurrences. I could provide a MatchEvaluator delegate that returns the matched string after the first replacement, rendering no change, but that sounds very inefficient to me.

看起来是。net Regex。替换方法自动替换所有匹配的匹配。我可以提供一个MatchEvaluator委托,该委托在第一次替换之后返回匹配的字符串,不显示任何更改,但对我来说这听起来非常低效。

What is the most efficient way to stop after the first replacement?

在第一次换货后停止的最有效方法是什么?

5 个解决方案

#1


27  

From MSDN:

从MSDN:

Replace(String, String, Int32)   

Within a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

在指定的输入字符串中,用指定的替换字符串替换匹配正则表达式模式的指定最大字符串数。

Isn't this what you want?

这不是你想要的吗?

#2


25  

Just to answer the original question... The following regex matches only the first instance of the word foo:

为了回答最初的问题……下面的regex只匹配单词foo的第一个实例:

(?<!foo.*)foo

(? < ! foo . *)foo

This regex uses the negative lookbehind (?<!) to ensure no instance of foo is found prior to the one being matched.

这个regex使用负的lookbehind(?

#3


3  

You were probably using the static method. There is no (String, String, Int32) overload for that. Construct a regex object first and use myRegex.Replace.

您可能使用了静态方法。没有(String, String, Int32)重载。首先构造regex对象并使用myRegex.Replace。

#4


0  

In that case you can't use:

在这种情况下,你不能使用:

string str ="abc546_$defg";
str = Regex.Replace(str,"[^A-Za-z0-9]", "");

Instead you need to declare new Regex instance and use it like this:

相反,您需要声明新的Regex实例,并这样使用:

string str ="abc546_$defg";
Regex regx = new Regex("[^A-Za-z0-9]");
str = regx.Replace(str,"",1)

Notice the 1, It represents the number of occurrences the replacement should occur.

注意1,它表示替换发生的次数。

#5


0  

Instead of creating a new Regex you can also do

除了创建新的Regex,您还可以这样做

Regex.Replace(originalString, "^"+pattern, stringToReplaceWith);

正则表达式。替换(originalString,“^”+模式,stringToReplaceWith);

to replace first occurrence.

来代替第一次出现。

#1


27  

From MSDN:

从MSDN:

Replace(String, String, Int32)   

Within a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

在指定的输入字符串中,用指定的替换字符串替换匹配正则表达式模式的指定最大字符串数。

Isn't this what you want?

这不是你想要的吗?

#2


25  

Just to answer the original question... The following regex matches only the first instance of the word foo:

为了回答最初的问题……下面的regex只匹配单词foo的第一个实例:

(?<!foo.*)foo

(? < ! foo . *)foo

This regex uses the negative lookbehind (?<!) to ensure no instance of foo is found prior to the one being matched.

这个regex使用负的lookbehind(?

#3


3  

You were probably using the static method. There is no (String, String, Int32) overload for that. Construct a regex object first and use myRegex.Replace.

您可能使用了静态方法。没有(String, String, Int32)重载。首先构造regex对象并使用myRegex.Replace。

#4


0  

In that case you can't use:

在这种情况下,你不能使用:

string str ="abc546_$defg";
str = Regex.Replace(str,"[^A-Za-z0-9]", "");

Instead you need to declare new Regex instance and use it like this:

相反,您需要声明新的Regex实例,并这样使用:

string str ="abc546_$defg";
Regex regx = new Regex("[^A-Za-z0-9]");
str = regx.Replace(str,"",1)

Notice the 1, It represents the number of occurrences the replacement should occur.

注意1,它表示替换发生的次数。

#5


0  

Instead of creating a new Regex you can also do

除了创建新的Regex,您还可以这样做

Regex.Replace(originalString, "^"+pattern, stringToReplaceWith);

正则表达式。替换(originalString,“^”+模式,stringToReplaceWith);

to replace first occurrence.

来代替第一次出现。