Solved

c# substring

Posted on 2004-04-01
14
1,329 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Long URL in SMS 6 31
Please explain "Multi-Tenant Services" 5 60
scanning dentists xray (the small ones) 3 41
how to read json value 2 27
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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!
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

937 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

8 Experts available now in Live!

Get 1:1 Help Now