需要使用正则表达式解析文本 [英] Need to parse text using Regular expression
问题描述
我如何解析使用正则表达式识别xml标签的文本。
亲爱的< customer>
这是为了让您知道您的以下门票已经确认。
<订单>
< OrderNo> <订单类型> < OrderAmount>
<订单>
总金额<总计>
感谢您与我们合作。
最好的问候
团队
我的匹配应该返回以下
客户
订单
orderNo
OrderAmount
总计
正在尝试使用的正则表达式仅返回第一场比赛。
How could i parse below text identifying xml tags using regular expression.
Dear <customer>
This is to let you know that your following Tickets have been confirmed.
<Orders>
<OrderNo> <OrderType> <OrderAmount>
<Orders>
Total Amount is <Total>
Thanks for doing business with us.
Best Regards
Team
My matches should return following
Customer
Orders
orderNo
OrderAmount
Total
The regular expression i am trying to use return only first match.
static void Main(string[] args)
{
//string source = "Test <TEST/>asda 9 <asdas/>";
string source = "Test <aaa> 2323 </aaa> cvcvc <bbb>asds2323 </bbb>sas";
//string regExp = @"(<\w+\/>)";
//string regExp = @"(<.*<\/\w+>)+";
string regExp = @"(<\w+>\s\w+\s<\/\w+>)+";
Regex regx = new Regex(regExp);
MatchCollection collection = Regex.Matches(source, regExp);
for (int index = 0; index < collection.Count; index++)
{
string s = collection[index].Value;
}
}
推荐答案
首先简化你的正则表达式:
Start by simplifying your regex:
<.+?>
只会抓住你想要的东西。
然后,我会这样做:
Will catch just the bits you want.
Then, I'd do it like this:
private void myButton_Click(object sender, EventArgs e)
{
string source = @"Dear <customer>
This is to let you know that your following Tickets have been confirmed.
<Orders>
<OrderNo> <OrderType> <OrderAmount>
<Orders>
Total Amount is <Total>
Thanks for doing business with us.
Best Regards
Team";
string regExp = @"<.+?>";
Regex regx = new Regex(regExp);
string newData = regx.Replace(source, ReplaceData);
}
public string ReplaceData(Match m)
{
switch (m.Value)
{
default: return m.Value;
case "<Orders>": return "----ORDERS DETAIL----";
case "<OrderNo>": return "----ORDER NO----";
...
}
}
尝试一下,你会明白我的意思!
Try it and you'll see what I mean!
这篇关于需要使用正则表达式解析文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!