?
Solved

Regular Expression line starts with 3 or 4

Posted on 2008-02-09
7
Medium Priority
?
507 Views
Last Modified: 2012-05-07
Hi,
 I am just learning regular expressions- can anyone help with this one? I am writing in C#.

I have a huge file and I want to extract out the data directory listed on lines that start with the number 3 or 4 and then a space.

sooooo....

3           CAROL B                                              1127872     20080208    ed/data/1127872/0001225208-08-002317.txt        
31           CAROL B                                              1127872     20080208    ed/data/1127872/0001225208-08-002318.txt        
4           CAROL B                                              1127872     20080208    ed/data/1127872/0001225208-08-002319.txt        

could return a matchcollection containing
data/1127872/0001225208-08-002317.txt
data/1127872/0001225208-08-002319.txt

Can anyone help??
0
Comment
Question by:toby_lang
7 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 20860491
^[34]\s
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20860561
In Perl, I would do something like this:

#!/usr/bin/perl -w
use strict;
my $collection;

while (<DATA>) {
    next unless (/^[34]\s/);
    /ed\/(.+)/;
    $collection .= $1;
}

print $collection;

0
 
LVL 10

Expert Comment

by:oleber
ID: 20860606
Using perl to get just the lines starting by 3 or 4 from a file

perl -ne 'print if /^[34]/' filename.txt

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 27

Accepted Solution

by:
ddrudik earned 1000 total points
ID: 20860716
To arrive at your intended MatchCollection (apparently trimming the ed/ from the paths):
^[34]\s.*?ed/(.*) *

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(@"^[34]\s.*?ed/(.*) *",RegexOptions.Multiline);
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx = 0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[" + mIdx + "][" + re.GetGroupNames()[gIdx] + "] = " + m.Groups[gIdx].Value);
              }
          }
        }
    }
}

Open in new window

0
 
LVL 10

Expert Comment

by:oleber
ID: 20860755
perl -ne 'print if /^[34]\s/' filename.txt
0
 

Author Closing Comment

by:toby_lang
ID: 31618354
Wow thanks very much for the help, that would have taken me forever to figure out- I had the first part the same as you but was really struggling with the second half. Thanks very much!
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20862445
Thanks for the question and the points.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

588 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