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

Tricky Perl Regurlar

Expand Messages
  • Ramprasad A Padmanabhan
    Hi I have got a string like this 0, hello world s people 0, It is for me 0, It s not for me Every string enclosed by quotes has a 0,
    Message 1 of 4 , Nov 30, 2001
    • 0 Attachment
      Hi
      I have got a string like this
      0,'hello world\'s people' 0,'It is for me' 0,'It\'s not for me'

      Every string enclosed by quotes has a 0, before it .

      I want a regular expression which will create an array of all strings within quotes

      I tried like this
      $str=~s/0,\'(.*?)\'/{ push @allvals,$1}/ge;
      this works fine but fails if the string itself has a quote sign

      I have written a work around ( and am very unhappy about it )

      Anyone can give me a better Idea










      #!/usr/bin/perl
      $str = <DATA>;
      $REPSTR="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      print "STR= $str\n";
      $str=~s/\\\'/$REPSTR/g;
      print "STR= $str\n";


      $str=~s/0,\'(.*?)\'/{ push @allvals,resub($1)}/ge;

      print join("\n",@allvals)."\n";

      sub resub {
      my($str)=@_;
      $str=~s/$REPSTR/\'/g;
      return $str;
      }

      __DATA__
      0,'hello world\'s people' 0,'It is for me' 0,'It\'s not for me'
    • Trap 13
      ... This seems to work as you expected (it prints what you want to go in your array) : #!/usr/bin/perl -w use strict; while ( ) { while
      Message 2 of 4 , Dec 2, 2001
      • 0 Attachment
        On Fri, Nov 30, 2001 at 02:01:42PM -0500, Ramprasad A Padmanabhan wrote:
        > Hi
        > I have got a string like this
        > 0,'hello world\'s people' 0,'It is for me' 0,'It\'s not for me'
        >
        > Every string enclosed by quotes has a 0, before it .
        >
        > I want a regular expression which will create an array of all strings within quotes
        >
        > I tried like this
        > $str=~s/0,\'(.*?)\'/{ push @allvals,$1}/ge;
        > this works fine but fails if the string itself has a quote sign
        >
        > I have written a work around ( and am very unhappy about it )
        >
        > Anyone can give me a better Idea
        >

        This seems to work as you expected (it prints what you want to go in your array) :

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


        while (<DATA>) {
        while (m/0,'([^'\\]*(?:\\.[^'\\]*)*)'/g ) {
        print $1, "\n";
        }
        }


        __DATA__

        0,'hello world\'s people' nothing to do with this 0,'It is for me' don\'t get this 0,'It\'s not for me'


        --
        Ce qu'il y a d'enivrant dans le mauvais goût, c'est le plaisir
        aristocratique de déplaire.

        Charles Baudelaire - Mon coeur mis à nu.
      • ramprasad@netcore.co.in
        This works Great And I am shattered I didnot even understand the RE . Please can you explain . Especially what is meant by m/0 in reqular exps ... people
        Message 3 of 4 , Dec 3, 2001
        • 0 Attachment
          This works Great

          And I am shattered I didnot even understand the RE . Please can you
          explain . Especially what is meant by m/0 in reqular exps


          --- In perl-beginner@y..., Trap 13 <bugbuilder@f...> wrote:
          > On Fri, Nov 30, 2001 at 02:01:42PM -0500, Ramprasad A Padmanabhan
          wrote:
          > > Hi
          > > I have got a string like this
          > > 0,'hello world\'s
          people' 0,'It is for me' 0,'It\'s not for me'
          > >
          > > Every string enclosed by quotes has a 0,
          before it .
          > >
          > > I want a regular expression which will create an array of all
          strings within quotes
          > >
          > > I tried like this
          > > $str=~s/0,\'(.*?)\'/{ push @allvals,$1}/ge;
          > > this works fine but fails if the string itself has a quote
          sign
          > >
          > > I have written a work around ( and am very unhappy about
          it )
          > >
          > > Anyone can give me a better Idea
          > >
          >
          > This seems to work as you expected (it prints what you want to go in
          your array) :
          >
          > #!/usr/bin/perl -w
          > use strict;
          >
          >
          > while (<DATA>) {
          > while (m/0,'([^'\\]*(?:\\.[^'\\]*)*)'/g ) {
          > print $1, "\n";
          > }
          > }
          >
          >
          > __DATA__
          >
          > 0,'hello world\'s people' nothing to do with this 0,'It is for me'
          don\'t get this 0,'It\'s not for me'
          >
          >
          > --
          > Ce qu'il y a d'enivrant dans le mauvais goût, c'est le plaisir
          > aristocratique de déplaire.
          >
          > Charles Baudelaire - Mon coeur mis à nu.
        • Trap 13
          ... m just means match, like s means substitute. // encapsulates the regex 0 is the first character of the regex, it is your main fields separator followed by
          Message 4 of 4 , Dec 4, 2001
          • 0 Attachment
            On Tue, Dec 04, 2001 at 06:06:01AM -0000, ramprasad@... wrote:
            > This works Great
            >
            > And I am shattered I didnot even understand the RE . Please can you
            > explain . Especially what is meant by m/0 in reqular exps
            >
            >
            > > m/0,'([^'\\]*(?:\\.[^'\\]*)*)'/g

            m just means match, like s means substitute.

            // encapsulates the regex

            0 is the first character of the regex, it is your main fields separator
            followed by ,'

            parentheses will capture the matching text

            (?:) will group an expression without capturing the text

            g allows to restart the expression after each success position

            And the remainder ... it's a little bit more complicated

            1)

            [^'\\]* will match all characters different from ' and \
            \ needs to be escaped

            2)
            when the previous expression fails, it's because it encounters :

            - ' : it is finished (?:\\.[^'\\]*)*) matches nothing and the
            following ' succeeds

            - \ followed by a any character and characters [^'\\]* different
            from ' and \ -> when this expression fails, go to step 2)

            This expression is described in a famous book "Mastering Regular
            Expressions" (by Jeff Friedl).

            You can note that it doesn't really works as written in the book,
            because it can start at an escaped ', what is not correct, you can try
            it with the example I gave by removing the 0, at the beginning of the
            regex.


            --
            Ce qu'il y a d'enivrant dans le mauvais goût, c'est le plaisir
            aristocratique de déplaire.

            Charles Baudelaire - Mon coeur mis à nu.
          Your message has been successfully submitted and would be delivered to recipients shortly.