Solved

c# substring

Posted on 2004-04-01
14
1,333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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