Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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;
            }
        }
Posted
Comments
Arjsrya 2-Jan-15 5:20am    
Is it XML right?Why are you not using Linq to read data from the XML?.
_Asif_ 2-Jan-15 5:29am    
This is a light weight library which replaces these codes in one parse. XML will complex the resolution process and requires looping
Arjsrya 2-Jan-15 5:37am    
which is light weight library?Does your xml gonna have long data like more orders or More customer information?

1 solution

Start by simplifying your regex:
<.+?>
Will catch just the bits you want.
Then, I'd do it like this:
C#
        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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900