Professional Web Applications Themes

perl regex to array - PERL Beginners

Hi, I have slightly a tricky situation, in my large program. I am trying the best to reproduce it I have a string like this $x='a{1}b{21}c{5}d'; # The numbers in the {} are random and are not of interest I want to access all elements from the string 'a' 'b' 'c' & 'd' How do I do it best ? I am now doing this $x .='{0}'; # So that the next regex works while($x=~/(.+?)\{\d+\}/){ print $1; ... } Is there a better way Thanks Ram...

  1. #1

    Default perl regex to array

    Hi,

    I have slightly a tricky situation, in my large program. I am trying the
    best to reproduce it


    I have a string like this
    $x='a{1}b{21}c{5}d';
    # The numbers in the {} are random and are not of interest

    I want to access all elements from the string 'a' 'b' 'c' & 'd'
    How do I do it best ?

    I am now doing this

    $x .='{0}'; # So that the next regex works

    while($x=~/(.+?)\{\d+\}/){
    print $1;
    ...
    }


    Is there a better way
    Thanks
    Ram









    Ramprasad Guest

  2. #2

    Default Re: perl regex to array

    Ramprasad A Padmanabhan said: 

    Will they always be letters? if so, I would change (.+?) to ([a-z]) or
    something similar (can't remember if the [] menas exclude or not, but you
    get the idea).
     

    Gavin Guest

  3. #3

    Default Re: perl regex to array

    From: Ramprasad A Padmanabhan <com> 

    If you are only interested in the letters you may do this:

    while ($x =~ /([a-zA-Z]+)/g {
    print $1;
    ...
    }

    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

  4. #4

    Default Re: perl regex to array

    On Oct 4, Ramprasad A Padmanabhan said:
     

    I'd do:

    my parts = split /{\d+}/, $string;

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    http://japhy.perlmonk.org/ % have long ago been overpaid?
    http://www.perlmonks.org/ % -- Meister Eckhart

    Jeff Guest

Similar Threads

  1. regex in perl.
    By Rod in forum PERL Beginners
    Replies: 2
    Last Post: June 18th, 03:38 PM
  2. Perl Regex and Arrays
    By Kevin Barry in forum PERL Beginners
    Replies: 0
    Last Post: October 12th, 04:54 PM
  3. Why does perl think there's a regex?
    By Chris in forum PERL Miscellaneous
    Replies: 4
    Last Post: September 4th, 04:02 PM
  4. regex diffs between perl 5.6.1 and 5.8.0?
    By Patrick Flaherty in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 18th, 09:03 PM

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