获取两个已知(或未知)字符串之间的字符串 [英] Get string between two known (or unknown) strings

查看:204
本文介绍了获取两个已知(或未知)字符串之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数来获取

"<Color...>(string I need)</Color>"


之间的字符串 点(...)并不重要.
如何在C#中使用Regex获取字符串.我的字符串与此相同:

我尝试过的事情:


the Dots(...) is not important.
how can get strings with Regex in C#.I have strings same as these:

What I have tried:

<Color>Black</Color>
<Color name=hair>Brown</Color>
<Color name=eye>blue</Color>

推荐答案

尝试:
(?<=\<.*?\>)(.*?)(?=\<.*?\>)


除了解决方案1.

只是一些有趣的链接,可帮助您构建和调试RegEx.
这是RegEx文档的链接:
perlre-perldoc.perl.org [ .NET Regex测试器-Regex Storm [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:学习,构建和开发&测试RegEx [^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这段代码向您展示了RegEx,它是一个很好的图形,它对理解RegEx的工作很有帮助: Debuggex:在线可视化regex测试器. JavaScript,Python和PCRE. [ ^ ]
该网站还以漂亮的图形显示了Regex,但无法测试与RegEx匹配的内容: Regexper [
In addition to Solution 1.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can''t test what match the RegEx: Regexper[^]


由于您的输入数据看起来是常规XML字符串的一部分,因此您也可以考虑使用C#类进行XML解析,例如,参见:使用C#进行XML简介-完整的C#教程 [ ^ ].
Since your input data looks part of a regular XML string, you could also consider working with the C# classes for XML parsing, see, for instance: Introduction to XML with C# - The complete C# tutorial[^].


这篇关于获取两个已知(或未知)字符串之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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