Solved

c# substring

Posted on 2004-04-01
14
1,332 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

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!

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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