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

regexp

Expand Messages
  • Nancy Pettigrew
    Regular expression question... I d like to extract text from a line, from between the symbols [ and ] . I could do multiple splits on the line, but is the a
    Message 1 of 9 , Jul 12, 2000
      Regular expression question...
      I'd like to extract text from a line, from between the symbols "[" and "]".
      I could do multiple splits on the line, but is the a better way to do this?
    • Andy Dougherty
      ... If you wish to do-it-yourself, you might find it useful to check out the comma-separated example in perlfaq4 (perldoc perlfaq4). Depending on the exact
      Message 2 of 9 , Jul 12, 2000
        On Wed, 12 Jul 2000, Nancy Pettigrew wrote:

        > Regular expression question...
        > I'd like to extract text from a line, from between the symbols "[" and "]".
        > I could do multiple splits on the line, but is the a better way to do this?

        If you wish to do-it-yourself, you might find it useful to check out the
        comma-separated example in perlfaq4 (perldoc perlfaq4). Depending on the
        exact requirements (e.g. can "[" or "]" ever be in the strings? Is there
        a possibility of bad data with mismatched delimiters? Can delimiters be
        nested? Can input span more than one line?) this apparently simple
        problem can grow complex.

        Alternatively, there are several modules to do this sort of thing. For
        example, if you go to

        http://theory.uwinnipeg.ca/search/cpan-search.html

        you can search for 'module name or description' and enter in 'delimit'
        you'll find several possibilities.

        Hope this helps,

        Andy Dougherty doughera@...
        Dept. of Physics
        Lafayette College, Easton PA 18042
      • David E Newman
        ... Give a sample of what s in your file.
        Message 3 of 9 , Jul 12, 2000
          On Wed, 12 Jul 2000, Nancy Pettigrew wrote:

          > Regular expression question...
          > I'd like to extract text from a line, from between the symbols "[" and "]".
          > I could do multiple splits on the line, but is the a better way to do this?

          Give a sample of what's in your file.
        • Weder Prado
          Hi, I am learning perl and I need to create a regexp that extract only the 4 digits after the - Below the string: 20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1
          Message 4 of 9 , Nov 4, 2011
            Hi,
            I am learning perl and I need to create a regexp that extract only the 4
            digits after the "-"
            Below the string:

            20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074 49=FIX134800
            52=20111014-18:00:14.474 56=BVMF 112=TEST 10=191

            Is it possible?


            [Non-text portions of this message have been removed]
          • David Precious
            ... Nearly anything is possible :) What part of the string are you talking about? the 18:00 after the - ? If not, please provide an example of what you
            Message 5 of 9 , Nov 4, 2011
              On Friday 04 November 2011 19:46:34 Weder Prado wrote:
              > Hi,
              > I am learning perl and I need to create a regexp that extract only the 4
              > digits after the "-"
              > Below the string:
              >
              > 20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074 49=FIX134800
              > 52=20111014-18:00:14.474 56=BVMF 112=TEST 10=191
              >
              > Is it possible?

              Nearly anything is possible :)

              What part of the string are you talking about? the "18:00" after the "-"? If
              not, please provide an example of what you expect to capture.

              You could quite easily use split to split the string on the '-', then substr
              to fetch the characters you want.

              Alternatively, e.g.:

              my ($time) = $foo =~ /-(\d{2}:\d{2})/;

              ... would assign the '18:00' to $time, if that's what you were looking for.
            • Shawn H Corey
              ... Yes, try: m{ # start of match - # match a hyphen D* # skip non-digits ( d) # capture first digit D* # skip non-digits ( d) # capture
              Message 6 of 9 , Nov 4, 2011
                On 11-11-04 03:46 PM, Weder Prado wrote:
                > I am learning perl and I need to create a regexp that extract only the 4
                > digits after the "-"
                > Below the string:
                >
                > 20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074 49=FIX134800
                > 52=20111014-18:00:14.474 56=BVMF 112=TEST 10=191
                >
                > Is it possible?

                Yes, try:

                m{ # start of match
                \- # match a hyphen
                \D* # skip non-digits
                (\d) # capture first digit
                \D* # skip non-digits
                (\d) # capture second digit
                \D* # skip non-digits
                (\d) # capture third digit
                \D* # skip non-digits
                (\d) # capture fourth digit
                }msx # end of match


                See:
                perldoc perlretut
                perldoc perlre


                --
                Just my 0.00000002 million dollars worth,
                Shawn

                Confusion is the first step of understanding.

                Programming is as much about organization and communication
                as it is about coding.

                The secret to great software: Fail early & often.

                Eliminate software piracy: use only FLOSS.

                "Make something worthwhile." -- Dear Hunter
              • Yatin Jadhav
                Try this it works........... $a = 20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074 49=FIX13480052=20111014-18:00:14.474 56=BVMF 112=TEST 10=191 ; @b =
                Message 7 of 9 , Nov 5, 2011
                  Try this it works...........

                  $a = "20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074 49=FIX13480052=20111014-18:00:14.474 56=BVMF 112=TEST 10=191";
                  @b = split(m/-/,$a);
                  @c = split(m//,$b[1]);
                  print ("$c[0]$c[1]$c[2]$c[3]\n");



                   
                  Regards,
                  Yatin Jadhav



                  ________________________________
                  From: Shawn H Corey <shawnhcorey@...>
                  To: perl-beginner@yahoogroups.com
                  Sent: Saturday, November 5, 2011 3:10 AM
                  Subject: Re: [PBML] regexp


                   
                  On 11-11-04 03:46 PM, Weder Prado wrote:
                  > I am learning perl and I need to create a regexp that extract only the 4
                  > digits after the "-"
                  > Below the string:
                  >
                  > 20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074 49=FIX134800
                  > 52=20111014-18:00:14.474 56=BVMF 112=TEST 10=191
                  >
                  > Is it possible?

                  Yes, try:

                  m{ # start of match
                  \- # match a hyphen
                  \D* # skip non-digits
                  (\d) # capture first digit
                  \D* # skip non-digits
                  (\d) # capture second digit
                  \D* # skip non-digits
                  (\d) # capture third digit
                  \D* # skip non-digits
                  (\d) # capture fourth digit
                  }msx # end of match

                  See:
                  perldoc perlretut
                  perldoc perlre

                  --
                  Just my 0.00000002 million dollars worth,
                  Shawn

                  Confusion is the first step of understanding.

                  Programming is as much about organization and communication
                  as it is about coding.

                  The secret to great software: Fail early & often.

                  Eliminate software piracy: use only FLOSS.

                  "Make something worthwhile." -- Dear Hunter



                  [Non-text portions of this message have been removed]
                • timothy adigun
                  Hi Prado, ... output, instead, the output will be 18:0. I suppose you didn t put the *:* into consideration as part of the output, since you are you *split* on
                  Message 8 of 9 , Nov 5, 2011
                    Hi Prado,

                    Yatin Jadhav <yatinjdhv@...> wrote:

                    > **
                    >
                    >
                    > Try this it works...........
                    >
                    > $a = "20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074
                    > 49=FIX13480052=20111014-18:00:14.474 56=BVMF 112=TEST 10=191";
                    > @b = split(m/-/,$a);
                    > @c = split(m//,$b[1]);
                    > print ("$c[0]$c[1]$c[2]$c[3]\n");
                    >
                    > print ("$c[0]$c[1]$c[2]$c[3]\n"); will not print 18:00, if that is the
                    output, instead, the output will be 18:0. I suppose you didn't put the *:*
                    into consideration as part of the output, since you are you *split* on the
                    $b[1] according to your script.

                    You could also write *print ("$c[0]$c[1]$c[2]$c[3]\n"); * more clearly as
                    *print @c[ 0 .. 4 ];*, which i believe will be more error prone.

                    Do one really have to go through all the trouble of array and split when,
                    one could just use substitution and regex to extract what you want? Anyway,
                    I guess, there is more than one way to do it!
                    Try this:

                    #!/usr/bin/perl -wl
                    use strict;

                    my $a =
                    "20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074
                    49=FIX13480052=20111014-18:00:14.474 56=BVMF 112=TEST 10=191";

                    ( my $new_val = $a ) =~ s/^(\d+)-(\d{2}:\d{2})(?:.*)/$2/imgx;
                    print $new_val;
                    __END__
                    OUTPUT: 18:00
                    --
                    Tim


                    [Non-text portions of this message have been removed]
                  • timothy adigun
                    ... output, instead, the output will be 18:0. I suppose you didn t put the *:* into consideration as part of the output, since you are you *split* on the $b[1]
                    Message 9 of 9 , Nov 5, 2011
                      Yatin Jadhav <yatinjdhv@...> wrote:

                      >
                      >
                      > Try this it works...........
                      >
                      > $a = "20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074
                      > 49=FIX13480052=20111014-18:00:14.474 56=BVMF 112=TEST 10=191";
                      > @b = split(m/-/,$a);
                      > @c = split(m//,$b[1]);
                      > print ("$c[0]$c[1]$c[2]$c[3]\n");
                      >
                      > print ("$c[0]$c[1]$c[2]$c[3]\n"); will not print 18:00, if that is the
                      output, instead, the output will be 18:0. I suppose you didn't put the *:*
                      into consideration as part of the output, since you are you *split* on the
                      $b[1] according to your script.

                      You could also write *print ("$c[0]$c[1]$c[2]$c[3]\n"); * more clearly as
                      *print @c[ 0 .. 4 ];*, which i believe will be more error prone free.

                      Do one really have to go through all the trouble of array and split when,
                      one could just use substitution and regex to extract what you want? Anyway,
                      I guess, there is more than one way to do it!
                      Try this:

                      #!/usr/bin/perl -wl
                      use strict;

                      my $a =

                      "20111014-18:00:14.474 : 8=FIX.4.2 9=68 35=1 34=1074
                      49=FIX13480052=20111014-18:00:14.474 56=BVMF 112=TEST 10=191";

                      ( my $new_val = $a ) =~ s/^(\d+)-(\d{2}:\d{2})(?:.*)/$2/imgx;
                      print $new_val;
                      __END__
                      OUTPUT: 18:00
                      --
                      Tim


                      [Non-text portions of this message have been removed]
                    Your message has been successfully submitted and would be delivered to recipients shortly.