从字符串中提取2子 [英] Extract two substrings from a string

查看:137
本文介绍了从字符串中提取2子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的,并从指定的字符串优化京顺路提取子。



我主要的字符串是像

 字符串str =< ABCMSG>< T> ACK< / T>< T> AAA0< / T>< T> BBBB1< / T>< / ABCMSG> ;; 



其中的值 AAA0 BBBB1 不contstant。它是从什么地方收集到的动态值。



我需要提取AAA0和BBBB1这里。



请建议我如果任何功能或优化的方式来做到这一点。



谢谢!


解决方案

 字符串str = @< ABCMSG>< T> ACK< / T>< T> AAA0< / T>< T> BBBB1< / T>< / ABCMSG>中; 
VAR匹配= Regex.Matches(STR,@< T>(\w +)LT; \ / T>);

Console.WriteLine(比赛[1] .Groups [1]); //输出AAAA1
Console.WriteLine(比赛[2] .Groups [1]); //输出BBB2

这假定您的数据总是在 < T>< /吨方式> 标记,也可能会想要做一些错误的情况下,捕捉没有找到匹配


what is the best and optimise wayto extract substrings from a specified string.

my primary string is like

string str = "<ABCMSG><t>ACK</t><t>AAA0</t><t>BBBB1</t></ABCMSG>"; 

where the value AAA0 and BBBB1 are not contstant. it is dynamic value collected from somewhere.

i need to extract AAA0 and BBBB1 here.

please suggest me if any function or optimize way to do this.

thank you!

解决方案

string str = @"<ABCMSG><t>ACK</t><t>AAA0</t><t>BBBB1</t></ABCMSG>";
var matches = Regex.Matches(str, @"<t>(\w+)<\/t>");

Console.WriteLine(matches[1].Groups[1]);    // outputs "AAAA1"
Console.WriteLine(matches[2].Groups[1]);    // outputs "BBB2"

This assumes that your data are always inside a <t></t> tag, also you might want to do some error catching in case no matches are found.

这篇关于从字符串中提取2子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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