简体   繁体   English

正则表达式删除定界符之间的字符串

[英]Regex to remove string in between delimiters

I am trying to get rid of the delimited text. 我试图摆脱带分隔符的文本。

For Example: "this#101# is#102# a#103# test#104#" 例如:“ this#101#is#102#a#103#test#104#”

Result : "this is a test" 结果:“这是一个测试”

Tried the Following which didn;t Work 尝试了以下无效的方法

string Pattern = @"(?<=#).*(?=#;)";
string text = "this#101# is#102# a#103# test#104#";
text = Regex.Replace(text, Pattern, string.Empty);

Try: 尝试:

string Pattern = @"#.*?#";

Since you need to get rid of the # too, you need to specify them as matching part of the regex. 由于还需要删除# ,因此需要将它们指定为正则表达式的匹配部分。 Your current regex does not match the # . 您当前的正则表达式与#不匹配。

Also you need to make the .* match non-greedy by adding a trailing ? 您还需要通过添加尾随来使.*匹配非贪心? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM