[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Split regular expression in C#.net

Posted on 2008-11-11
5
Medium Priority
?
1,149 Views
Last Modified: 2012-05-05
i need to split the regular expression @"^(\S*)/([^/]+)-l(\d+).html" in C#.net
Example url is testing/testing1/whitebox-testing-l4.html
I need Group[1] = testing/testing1
Group[2] = whitebox-testing
Group[3] = 4
0
Comment
Question by:uvals
4 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 22930411

C#.NET Code Example:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          Regex re = new Regex(@"^(\S*)/([^/]+)-l(\d+)\.html");
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}
 
Matches Found:
[0][0] = testing/testing1/whitebox-testing-l4.html
[0][1] = testing/testing1
[0][2] = whitebox-testing
[0][3] = 4

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22948759
@"^(.*?)/([^/]+)-l(\d+).html"
0
 
LVL 2

Author Comment

by:uvals
ID: 22958041
This is the solution for query
@"^(.*)/([^/]+)-l([0-9]+).html"

Open in new window

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 23629355
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

867 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