[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Capturing everything before a \n

Posted on 2008-02-05
12
Medium Priority
?
565 Views
Last Modified: 2008-02-05
I attached the text I need to parse. What I need to do is capture each line individually (there should be 4) using regular expressions. Here is the regex I have now: \n.*
It will capture everything but not the top line.

1:5 1-19-08  flags 803BE180 hg0 10.936 hg2 3.864 hgt 14.800 rfint 75355.400 intt 28.175 rctt 45.453 prbt 0.000 cnvt 0.000 umbt 0.000 vntp -3.076 orfp -0.556 dilp -11.111 bbkp -11.111 edup -11.111 vac 33.933 smplf 0.000 pmtv 647.871 pres 626.082 dilf 20.000 obkg 4.810 tblg 6.304 ocoef 0.793 tcoef 1.343 hg81 0.000 lampt 43.446 oxyt 0.000 fsafe 0.000
1:6 1-19-08  flags 803BE180 hg0 10.936 hg2 3.864 hgt 14.800 rfint 75355.400 intt 28.175 rctt 45.453 prbt 0.000 cnvt 0.000 umbt 0.000 vntp -3.076 orfp -0.556 dilp -11.111 bbkp -11.111 edup -11.111 vac 33.933 smplf 0.000 pmtv 647.871 pres 626.082 dilf 20.000 obkg 4.810 tblg 6.304 ocoef 0.793 tcoef 1.343 hg81 0.000 lampt 43.446 oxyt 0.000 fsafe 0.000
1:7 1-19-08  flags 803BE180 hg0 10.936 hg2 3.864 hgt 14.800 rfint 75355.400 intt 28.175 rctt 45.453 prbt 0.000 cnvt 0.000 umbt 0.000 vntp -3.076 orfp -0.556 dilp -11.111 bbkp -11.111 edup -11.111 vac 33.933 smplf 0.000 pmtv 647.871 pres 626.082 dilf 20.000 obkg 4.810 tblg 6.304 ocoef 0.793 tcoef 1.343 hg81 0.000 lampt 43.446 oxyt 0.000 fsafe 0.000
1:8 1-19-08  flags 803BE180 hg0 10.936 hg2 3.864 hgt 14.800 rfint 75355.400 intt 28.175 rctt 45.453 prbt 0.000 cnvt 0.000 umbt 0.000 vntp -3.076 orfp -0.556 dilp -11.111 bbkp -11.111 edup -11.111 vac 33.933 smplf 0.000 pmtv 647.871 pres 626.082 dilf 20.000 obkg 4.810 tblg 6.304 ocoef 0.793 tcoef 1.343 hg81 0.000 lampt 43.446 oxyt 0.000 fsafe 0.000

Open in new window

0
Comment
Question by:DuNuNuBatman
  • 8
  • 4
12 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 20827384
.*

(where . would match any char except \n - as long as no RegexOptions.Singleline flag is set )
0
 

Author Comment

by:DuNuNuBatman
ID: 20827401
That matches everything. I need to have a MatchCollection consisting of each row seperated.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20827403
Note that there is also a flag called RegexOptions.Multiline that you could set to treat the ^$ boundaries per line.
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

Expert Comment

by:ddrudik
ID: 20827414
Try:
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(@".*");
          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);
              }
          }
        }
    }
}

Or:
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(@"^.*$",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);
              }
          }
        }
    }
}
0
 

Author Comment

by:DuNuNuBatman
ID: 20827441
Is there a way to do it using just regular expressions? The regex is part of the config file. I don't have access to the actual source code for the project at the moment.
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 20827502
If your "config file" app treats regular expressions as though RegexOptions.Singleline is set I would try:
.*?(?:\r\n|$)

Your last match will likely be empty.
0
 

Author Comment

by:DuNuNuBatman
ID: 20828082
That was almost exactly what I was looking for.
Here is the final expression: .*\r\n
It seems to be working right. Do you see any issue with this?
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20828608
.*\r\n would only catch the first lines 1-3, assuming that 4 does not end in \r\n.  That's what |$ is meant to catch.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20828614
If you concatenated chr(13) & chr(10) to the string before processing it, then .*\r\n would work fine (since that would add a \r\n to the last line).
0
 

Author Comment

by:DuNuNuBatman
ID: 20828629
got it, I'll make the changes to mine. Thanks!
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20828644
If you could specify something else about the start of the lines you could avoid conc. \r\n if you use something like:
\d+:.*?(?=\r\n|$)
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20828653
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

In this post we will learn different types of Android Layout and some basics of an Android App.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

590 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