Solved

c# substring

Posted on 2004-04-01
14
1,331 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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