正则表达式C#之间找到2已知值的字符串 [英] RegEx C# : Find a string between 2 known values

查看:222
本文介绍了正则表达式C#之间找到2已知值的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够提取字符串2标签之间,例如:从00002, morenonxmldata<标签1> 0002< /标签1> morenonxmldata

I need to be able to extract a string between 2 tags for example: "00002" from "morenonxmldata<tag1>0002</tag1>morenonxmldata"

在使用C#和.NET 3.5。

In using C# and .NET 3.5.

推荐答案

A 正则表达式 - 免费的解决方案:

A RegEx-free solution:

string ExtractString(string s, string tag) {
     // You should check for errors in real-world code, omitted for brevity
     var startTag = "<" + tag + ">";
     int startIndex = s.IndexOf(startTag) + startTag.Length;
     int endIndex = s.IndexOf("</" + tag + ">", startIndex);
     return s.Substring(startIndex, endIndex - startIndex);
}

这篇关于正则表达式C#之间找到2已知值的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆