Solved

c# substring

Posted on 2004-04-01
14
1,328 Views
Last Modified: 2010-07-27
hello,

i have sdfiopgjosdifj3476498764589<abc234>eruhiuwetheri9476948769834rjbheirbt<23423sdf>iojrojyriotyjie

what is the best way to extract the values inside the <> without using substring?

thank you.
0
Comment
Question by:alain123
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 1

Author Comment

by:alain123
ID: 10732602
something like this

String Extension(String url)
    {
        Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
            RegexOptions.Compiled);
        return r.Match(url).Result("${proto}${port}");
    }
0
 
LVL 1

Author Comment

by:alain123
ID: 10732840
or something like this might be better

void test()
            {
                  string text = "One car red car blue car";
                  string pat = @"(\w+)\s+(car)";
                  // Compile the regular expression.
                  Regex r = new Regex(pat, RegexOptions.IgnoreCase);
                  // Match the regular expression pattern against a text string.
                  Match m = r.Match(text);
                  int matchCount = 0;
                  while (m.Success)
                  {
                        Console.WriteLine("Match"+ (++matchCount));
                        for (int i = 1; i <= 2; i++)
                        {
                              Group g = m.Groups[i];
                              Response.Write("Group"+i+"='" + g + "'" + "<br>");
                              CaptureCollection cc = g.Captures;
                              for (int j = 0; j < cc.Count; j++)
                              {
                                    Capture c = cc[j];
                                    Response.Write("Capture"+j+"='" + c + "', Position="+c.Index  + "<br>");
                              }
                        }
                        m = m.NextMatch();
                  }
            }
0
 
LVL 3

Expert Comment

by:gillit
ID: 10732862
Is this what you are looking for?

Regex r = new Regex("(<[^>]+>)", RegexOptions.Compiled);
0
 
LVL 1

Author Comment

by:alain123
ID: 10732908
i have no idea.. would that extract the value between  my <> tags?

this is the url string text ="<A HREF=http://www.dell.com/help.asp?x=<row3>&y=<row1>><row2></A>"

i want to extract the values inside < ..row.. >

0
 
LVL 1

Author Comment

by:alain123
ID: 10733064
this is the answer

void test2()
            {

            
                        
                        //string Text  ="<number1>hello1<number2>Hello2<number3>";
                        string Text   =  "<A HREF=http://www.dell.com/help.asp?x=<row3>&y=<row1>><row2></A>";
                        Response.Write("text=" + Text );
                        Regex r = new Regex("<([a-z0-9]+)>", RegexOptions.IgnoreCase);
                        MatchCollection mc = r.Matches(Text);

                        foreach(Match m in  mc)
                        {
                         for( int i = 0;  i < m.Groups.Count ; i ++)
                                    {
                              
                                          Response.Write(m.Groups[i].ToString() + "<br>");
                                    }
                        }
                        

            }
0
 
LVL 3

Expert Comment

by:gillit
ID: 10733200
Good to hear you found your solution.

Just some explanations, I thought you might find useful.

Your solution will include any tags with a-z or 0-9. But it will not return tags that are like </body>, </html>, </head>, etc.

The solution I posted will give those. But if you are using this for web pages, it will also return values that are used in mathematical scripts. For example i < 2 OR j> 4. Basically, it will return all values between <>... for better or worse.

For that you would want to use <(?/[a-z0-9]+)>, to include the end tags.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:_TAD_
ID: 10733462


Why would you not want to use Find() and Substring()?

Using string functions is a lot faster than regex for small applications, its not until you do a process some 150-500 times that regex becomes substantially more efficient than string functions (there is a bit of overhead with the regex class).
0
 
LVL 3

Expert Comment

by:gillit
ID: 10734448
None from me.
0
 
LVL 1

Author Comment

by:alain123
ID: 10735250
thank you gillit, definetly will follow your advice.. tad, i think it's more secure to use regexps..
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10737422
alain>
  as a rule I would tend to agree that regex is the best route to go... for medium to large searches.


If you simply want to parse a domain\loginId, regex is overkill.... like taking a cannon to a gun fight
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10737426


btw... no objections.  Delete away.
0
 
LVL 1

Author Comment

by:alain123
ID: 10759155
thank you tad, i'll take it in consideration..
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 10761305
Closed, 400 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now