Professional Web Applications Themes

in a regex- re-arrange $1..$n - PERL Beginners

the following is an as yet incomplete regex that i'm working on: $data = $_ =~ s/<(name)>(.*?)<(desc)>(.*?)<(copy)>(.*?)<(license)> (.*?)<(entry)>//xg every other capture ($1 $3 $5 $7 $9) is going to be used one way, the other captures will be used differently... i'm wondering if there's a way to force the captures into particular numbered variable, so that $1 =$1 $2 = $3 $3 = $5 $4 = $7 $5 = $9 etc... or is it even worth it for easier coding later on? willy http://www.hackswell.com/corenth...

  1. #1

    Default in a regex- re-arrange $1..$n

    the following is an as yet incomplete regex that i'm working on:

    $data = $_ =~
    s/<(name)>(.*?)<(desc)>(.*?)<(copy)>(.*?)<(license)> (.*?)<(entry)>//xg


    every other capture ($1 $3 $5 $7 $9) is going to be used one way, the
    other
    captures will be used differently... i'm wondering if there's a way to
    force the captures into particular numbered variable, so that
    $1 =$1
    $2 = $3
    $3 = $5
    $4 = $7
    $5 = $9

    etc... or is it even worth it for easier coding later on?






    willy
    http://www.hackswell.com/corenth

    William Guest

  2. #2

    Default Re: in a regex- re-arrange $1..$n

    > the following is an as yet incomplete regex that i'm working on: 

    How about just not capturing them to begin with? If you add C<?:> to
    the beginning of your capture it will ignore the capture. Having said
    that, why are you capturing to begin with if you are just going to throw
    them away? The parens in your example don't matter for the match to work.
     

    At the very least I would switch to named variables to make it more
    readable and prevent the chance that they get clobbered by a later regex.

    http://danconia.org
    Wiggins Guest

  3. #3

    Default RE: in a regex- re-arrange $1..$n



     [/ref]
    s/<(name)>(.*?)<(desc)>(.*?)<(copy)>(.*?)<(license)> (.*?)<(entry)>//xg [/ref]
    to 
    >
    >How about just not capturing them to begin with? If you add C<?:> to
    >the beginning of your capture it will ignore the capture. Having said
    >that, why are you capturing to begin with if you are just going to[/ref]
    throw 
    work.


    you misread- i intend to USE all of the captures- but i wanted to
    categorize them more easily by "type", so ?: would be useless.
     
    >
    >At the very least I would switch to named variables to make it more
    >readable and prevent the chance that they get clobbered by a later[/ref]
    regex.

    what i am likely going to do is finish the processing in the one s///,
    so leaving them as is might not be a problem.


    i've worked on this since my last post::

    $_ =~ s/<(.+?)>(.*?)<(.+?)>(.*?)<(.+?)>(.*?)<(.+?)>(.*?)<e ntry>/
    $css{$1}{open} $2 $css{$1}{close}
    $css{$3}{open} $4 $css{$3}{close}
    $css{$5}{open} $6 $css{$5}{close}
    $css{$7}{open} $8 $css{$7}{close}/xg


    %css is an HoH- using half the captures as keys etc... looks like this
    will work well enough for me after all :)

    but i'm still wondering about the original question... with so much time
    spent by me with regexen, i'd love to keep them as readable and
    malleable
    as i can.




    willy
    http://www.hackswell.com/corenth
    William Guest

  4. #4

    Default Re: in a regex- re-arrange $1..$n

    On Mon, 11 Oct 2004 13:13:37 -0600, Wiggins d Anconia
    <org> wrote: [/ref]


    Are you thinking of :

    ( $a1,$b1,$a2,$b2,$a3,$b3,$a4,$b4,$a5,$b5 ) = $_ =~ /regexp/ ?

    The result of a regexp ($1..$9) are returned in array context, so you can
    either assign them to an array

    data = $_ =~ /regexp/

    or return them to named variables as above.

    I use the above, because I can't stand using '$1' '$2' '$3' etc for more than
    two lines of code below the regexp capture, because context is lost to
    the reader.

     
    >
    > How about just not capturing them to begin with? If you add C<?:> to
    > the beginning of your capture it will ignore the capture. Having said
    > that, why are you capturing to begin with if you are just going to throw
    > them away? The parens in your example don't matter for the match to work.

    >
    > At the very least I would switch to named variables to make it more
    > readable and prevent the chance that they get clobbered by a later regex.
    >
    > http://danconia.org
    >
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]
    David Guest

  5. #5

    Default RE: in a regex- re-arrange $1..$n


     

    that might be what i need- if i'm going to be working with these values
    alot, then i want to know what they are at the outset....
     
    can 
    more 


    exactly! i have enough trouble with the regex itself to worry about
    what the captures refer to.

    I'd forgotten about matches returning in a list context- no s///
    required.



    thanks,

    willy
    http://www.hackswell.com/corenth
    William Guest

Similar Threads

  1. Arrange anchored object
    By CJC_Williams@adobeforums.com in forum Adobe Indesign Windows
    Replies: 3
    Last Post: August 9th, 07:06 AM
  2. Won't let me arrange layers
    By Claudette_Helie in forum Macromedia Freehand
    Replies: 3
    Last Post: January 27th, 04:16 AM
  3. Desktop Icon Arrange
    By Perry G. Burda in forum Windows XP/2000/ME
    Replies: 0
    Last Post: July 14th, 07:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139