Loading ...
Sorry, an error occurred while loading the content.
 

Re: Antwort: [PBML] perl substitution

Expand Messages
  • Rob Biedenharn
    ... You need to use () s to capture the parts of the regular expression match that you re interested in keeping: For example: perl -w -e $line = [the time
    Message 1 of 5 , Jul 2, 2007
      On Jul 2, 2007, at 4:29 PM, Scott Mayo wrote:
      > I am going to ask this again since I don't think that I asked what I
      > needed to the first time. I tried the suggestions, but they did
      > not do
      > what I wanted. I do appreciate those helps though. I am going to
      > parse
      > a log file and here is what I basically want to do:
      >
      > I want to pull off the first bracketed info into a variable and leave
      > the rest in another variable.

      You need to use ()'s to capture the parts of the regular expression
      match that you're interested in keeping:

      For example:

      perl -w -e '
      $line = "[the time is 1:59:00] [Scott] How are you? [Allie] Fine
      today.";
      $line =~ m/^(\[[^]]*\])?(.*)$/;
      print "bracketed part: $1\n";
      print "remaining line: $2\n";
      '

      bracketed part: [the time is 1:59:00]
      remaining line: [Scott] How are you? [Allie] Fine today.

      > Here are a couple of other things. There may be only one set of
      > bracketed info and there may be none at all or there may be a lot of
      > sets of bracketed info. So I could have the following in the file
      > which
      > is 3 lines in total:
      >
      > [the time is 1:59:00] [Scott] How are you? [Allie] Fine today.
      > Thank you for coming.
      > [Tell] Hey there.
      >
      > After parsing my lines I could have the following info.
      >
      > $line1[1] = [the time is 1:59:00]
      > $line1[2] = [Scott] How are you? [Allie] Fine today.
      >
      > $line2[1] = Thank you for coming.
      >
      > $line3[1] = [Tell]
      > $line3[2] = Hey there.
      >
      > Thanks again.
      >
      > --
      > Scott Mayo
      > System Administrator
      > Bloomfield Schools

      I very nearly gave you a variation of the standard "don't ask us to
      do your homework" when I noticed your .k12. address, but I decided to
      give you the benefit of the doubt when I got to your signature.

      The '?' in the regexp means that the first part is optional (i.e.,
      can match zero or one time).

      You might also have to check "if (defined $1)" to know whether there
      was a "first part" or not.

      -Rob

      Rob Biedenharn http://agileconsultingllc.com
      Rob@...
    Your message has been successfully submitted and would be delivered to recipients shortly.