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

regular expression help

Expand Messages
  • Luca Ferrari
    Hi, supposing I ve got a string like the following: a.b.c.d.e.f and that I d like to keep only the first characters before the first . (e.g., a ), why the
    Message 1 of 13 , Aug 5, 2004
    • 0 Attachment
      Hi,
      supposing I've got a string like the following:
      a.b.c.d.e.f
      and that I'd like to keep only the first characters before the first
      '.' (e.g., 'a'), why the following is not working?

      $string =~ s/^(.+)\.(.*)/$1/;

      Applied to the above example string it gives me back only 'f'. Any
      explanation?

      Thanks,
      Luca


      --
      Luca Ferrari,
      fluca1978@...
    • Gordon Stewart
      ... untested - but try :- $string =~ s/^(.+?) .(.*)/$1/; you get f because F still precedes a . (dot)... put the ? in, to restrict it to the first match (or
      Message 2 of 13 , Aug 5, 2004
      • 0 Attachment
        On Thu, 5 Aug 2004 13:41:35 +0200, Luca Ferrari <fluca1978@...> wrote:

        > $string =~ s/^(.+)\.(.*)/$1/;
        >
        > Applied to the above example string it gives me back only 'f'. Any
        > explanation?
        >
        > Thanks, Luca

        untested - but try :-

        $string =~ s/^(.+?)\.(.*)/$1/;

        you get 'f' because F still precedes a . (dot)...

        put the ? in, to restrict it to the first match (or non-match)

        --

        G Stewart
        gmail-talk-help@yahoogroups.com
      • Madani, Srikanth, VF-DE
        Hi Luca, I m not sure if I get the requirement. If you wish to _retain_ the one character before the initial period, this is how I would do it: chomp (my
        Message 3 of 13 , Aug 5, 2004
        • 0 Attachment
          Hi Luca,

          I'm not sure if I get the requirement. If you wish to _retain_ the one character before the initial period, this is how I would do it:

          chomp (my $string = <>);
          if ($string =~ /^(.)\./) {
          $string = $1;
          print "\nThe string has been changed to $string\n";
          } else {
          print "\nThe string $string doesn't match the expected criterion.\n";
          }


          Best regards,
          Srikanth Madani


          -----Original Message-----
          From: Luca Ferrari [mailto:fluca1978@...]
          Sent: Thursday, August 05, 2004 12:42 PM
          To: perl-beginner@yahoogroups.com
          Subject: [PBML] regular expression help


          Hi,
          supposing I've got a string like the following:
          a.b.c.d.e.f
          and that I'd like to keep only the first characters before the first
          '.' (e.g., 'a'), why the following is not working?

          $string =~ s/^(.+)\.(.*)/$1/;

          Applied to the above example string it gives me back only 'f'. Any
          explanation?

          Thanks,
          Luca


          --
          Luca Ferrari,
          fluca1978@...
        • JORGE SILVA
          I think this code should work... #------------------------------------ my ($a, $b); $a = a.b.c.d.e.f. ; ($b) = split(/ ./,$a); print $b, n ;
          Message 4 of 13 , Aug 5, 2004
          • 0 Attachment
            I think this code should work...

            #------------------------------------
            my ($a, $b);
            $a = "a.b.c.d.e.f.";
            ($b) = split(/\./,$a);
            print $b, "\n";
            #------------------------------------

            Best Regards,

            Jorge Silva.

            | -----Original Message-----
            | From: Madani, Srikanth, VF-DE [mailto:srikanth.madani@...]
            | Sent: quinta-feira, 5 de Agosto de 2004 13:29
            | To: perl-beginner@yahoogroups.com
            | Subject: RE: [PBML] regular expression help
            |
            |
            | Hi Luca,
            |
            | I'm not sure if I get the requirement. If you wish to
            | _retain_ the one character before the initial period, this is
            | how I would do it:
            |
            | chomp (my $string = <>);
            | if ($string =~ /^(.)\./) {
            | $string = $1;
            | print "\nThe string has been changed to $string\n";
            | } else {
            | print "\nThe string $string doesn't match the
            | expected criterion.\n";
            | }
            |
            |
            | Best regards,
            | Srikanth Madani
            |
            |
            | -----Original Message-----
            | From: Luca Ferrari [mailto:fluca1978@...]
            | Sent: Thursday, August 05, 2004 12:42 PM
            | To: perl-beginner@yahoogroups.com
            | Subject: [PBML] regular expression help
            |
            |
            | Hi,
            | supposing I've got a string like the following:
            | a.b.c.d.e.f
            | and that I'd like to keep only the first characters before the first
            | '.' (e.g., 'a'), why the following is not working?
            |
            | $string =~ s/^(.+)\.(.*)/$1/;
            |
            | Applied to the above example string it gives me back only 'f'. Any
            | explanation?
            |
            | Thanks,
            | Luca
            |
            |
            | --
            | Luca Ferrari,
            | fluca1978@...
            |
            |
            | ------------------------ Yahoo! Groups Sponsor
            | --------------------~-->
            | Yahoo! Domains - Claim yours for only $14.70
            | http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/ndFolB/TM
            | --------------------------------------------------------------
            | ------~->
            |
            | Unsubscribing info is here:
            | http://help.yahoo.com/help/us/groups/groups-32.html
            | Yahoo! Groups Links
            |
            |
            |
            |
            |
            |


            This message may contain confidential and/or privileged information and is intended solely for the use of the individual or entity to whom is addressed. If you are not the intended recipient, then any disclosure, copying, distribution or any other action in reliance upon is expressly prohibited and may be unlawful. In this case, please advise the sender by replying and deleting this message.
            Any opinions or advices contained in this message or attachments are the sole responsibility of the sender and do not necessarily represent Banco Comercial Portugues.
            This e-mail message has been swept by Mimesweeper for the presence of computer viruses.
          • Brad Lhotsky
            Perl s + and * Quantifiers are greedy. In other words, they grab everything from the beginning of the line on through the end. I wrote a Regex primer a while
            Message 5 of 13 , Aug 5, 2004
            • 0 Attachment
              Perl's + and * Quantifiers are greedy.

              In other words, they grab everything from the beginning of the line on
              through the end. I wrote a Regex primer a while back:

              http://www.developer.com/lang/perl/article.php/3330231

              I know I covered greedy vs. non-greedy in there.

              and I'm sure Randal has atleast one column on this exact thing somewhere
              on his site at:
              http://www.stonehenge.com/merlyn/columns.html

              In this case, I'm thinking that you know exactly what you're not looking
              for, so it might be easier to look for that:

              #!/usr/bin/perl

              use strict;

              my @strings = qw(
              a.b.c.d.f.e.a
              sdg.sd.s..sdg.s.d
              321.asf..124124
              sfkjlgasfkakjsfasfasmf.asfaaga..
              .fasfasgasgasg
              45.asfagaxcb.
              );

              foreach my $string (@strings) {
              my ($match) = ($string =~ /^([^\.]{0,2})/);
              print "$string :: '$match'\n";
              }


              Enjoy.

              On Thu, Aug 05, 2004 at 01:41:35PM +0200, Luca Ferrari wrote:
              > Hi,
              > supposing I've got a string like the following:
              > a.b.c.d.e.f
              > and that I'd like to keep only the first characters before the first
              > '.' (e.g., 'a'), why the following is not working?
              >
              > $string =~ s/^(.+)\.(.*)/$1/;
              >
              > Applied to the above example string it gives me back only 'f'. Any
              > explanation?
              >
              > Thanks,
              > Luca
              >
              >
              > --
              > Luca Ferrari,
              > fluca1978@...
              >
              >
              >
              > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
              > Yahoo! Groups Links
              >
              >
              >
              >
              >

              --
              Brad Lhotsky <brad@...>
            • Brad Lhotsky
              For some reason, my initial reading of this post was the first two characters before the first . . Weird. Anyways, ignore my other code, do this: my $match =
              Message 6 of 13 , Aug 5, 2004
              • 0 Attachment
                For some reason, my initial reading of this post was the first two
                characters before the first '.'. Weird.

                Anyways, ignore my other code, do this:

                my $match = (split '.', $string)[0];

                On Thu, Aug 05, 2004 at 01:41:35PM +0200, Luca Ferrari wrote:
                > Hi,
                > supposing I've got a string like the following:
                > a.b.c.d.e.f
                > and that I'd like to keep only the first characters before the first
                > '.' (e.g., 'a'), why the following is not working?
                >
                > $string =~ s/^(.+)\.(.*)/$1/;
                >
                > Applied to the above example string it gives me back only 'f'. Any
                > explanation?
                >
                > Thanks,
                > Luca
                >
                >
                > --
                > Luca Ferrari,
                > fluca1978@...
                >
                >
                >
                > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
                > Yahoo! Groups Links
                >
                >
                >
                >
                >

                --
                Brad Lhotsky <brad@...>
              • Luca Ferrari
                On Thursday 05 August 2004 13:41 Luca Ferrari s cat walking on the keyboard ... Hi guys, thanks everybody for the help. The solution I adopted is $string =~
                Message 7 of 13 , Aug 5, 2004
                • 0 Attachment
                  On Thursday 05 August 2004 13:41 Luca Ferrari's cat walking on the keyboard
                  wrote:

                  > Hi,
                  > supposing I've got a string like the following:
                  > a.b.c.d.e.f
                  > and that I'd like to keep only the first characters before the first
                  > '.' (e.g., 'a'), why the following is not working?
                  >
                  > $string =~ s/^(.+)\.(.*)/$1/;
                  >
                  > Applied to the above example string it gives me back only 'f'. Any
                  > explanation?
                  >

                  Hi guys,
                  thanks everybody for the help.
                  The solution I adopted is
                  $string =~ s/^(.+?)\.(.*)/$1/;

                  I know that I could use split, but since perl has a regexp engine why don't
                  use it?

                  Thanks everybody,
                  Luca

                  --
                  Luca Ferrari,
                  fluca1978@...
                Your message has been successfully submitted and would be delivered to recipients shortly.