简体   繁体   English

正则表达式以匹配C#中的模式

[英]Regex to match pattern in C#

I have a string in the following format 我有以下格式的字符串

ABC=23:Qasd=56:Def=40.44

I would like to replace all the strings ( ABC= , Qasd= and Def= ) with empty string. 我想用空字符串替换所有字符串( ABC=Qasd=Def= )。 The string after = can be anything. =之后的字符串可以是任何值。 So my output string would be 所以我的输出字符串是

23:56:40.44

It would be great if you can let me know the regex for that in C# 如果您可以让我知道C#中的正则表达式,那就太好了

(^|:)[^=]*=

replaced with 替换为

$1

Matches the beginning of a string or a : and everything until and including = . 匹配字符串或:的开头,以及直到=包括在内的所有内容。 It is replaced with $1 to keep : . $1代替:

C# C#

string strTargetString = @"ABC=23:Qasd=56:Def=40.44";

var myRegex = new Regex(@"(^|:)[^=]*=");    
var result = myRegex.Replace(strTargetString, @"$1");

//result: 23:56:40.44

More examples: 更多示例:

ABC=hello:Qasd=56:Def=40.44 => hello:56:40.44

Match 比赛

^[^=]+=|(?<=:)[^=]+=

and replace with string.Empty 并替换为string.Empty

Regex.Replace("ABC=23:Qasd=56:Def=40.44", @"^[^=]+=|(?<=:)[^=]+=", string.Empty);

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

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