Professional Web Applications Themes

Reg Exp - PERL Beginners

Dear Friends, I have the below code. my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-: my $del = ':-:'; # Delimeter my $b; while ($a ne $b) { $a =~ /^$b$del(.?)($del)?/; my $c = $1; print "$c\n"; $b .= $del . $c; } The above code is working fine. But when I change the text in $a like below $a = ":-:m:-:a:-:l:-::-:k"; # Here I removed the letter l between two '::'s Now the code is not working properly. Any help in this is appreciated. Note: There may be non-alpha numeric chars between ':-:'. Regards, Mallik....

  1. #1

    Default Reg Exp

    Dear Friends,

    I have the below code.

    my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
    my $del = ':-:'; # Delimeter
    my $b;

    while ($a ne $b)
    {
    $a =~ /^$b$del(.?)($del)?/;
    my $c = $1;
    print "$c\n";
    $b .= $del . $c;
    }

    The above code is working fine. But when I change the text in $a like below

    $a = ":-:m:-:a:-:l:-::-:k"; # Here I removed the letter l between two '::'s

    Now the code is not working properly.

    Any help in this is appreciated.

    Note: There may be non-alpha numeric chars between ':-:'.

    Regards,
    Mallik.

    Mallik Guest

  2. #2

    Default Re: Reg Exp

    Mallik wrote: 

    Hello,
     

    Perhaps you need to use split and join:

    $a = ':-:m:-:a:-:l:-::-:k';

    my $del = ':-:'; # Delimeter

    $b = join $del, split $del, $a, -1;



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  3. #3

    Default Re: Reg Exp

    Your use of (.?)($del)? does not do what you expect. In the case of
    :-:a:-: you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
    :-::-: you will find (.?) = ":" and ($del)? matches nothing.

    Check out 'look-ahead' matches for a way to solve this.



    On Tue, 05 Oct 2004 04:03:21 -0700, John W. Krahn <net> wrote: 
    >
    > Hello,

    >
    > Perhaps you need to use split and join:
    >
    > $a = ':-:m:-:a:-:l:-::-:k';
    >
    > my $del = ':-:'; # Delimeter
    >
    > $b = join $del, split $del, $a, -1;
    >
    > John
    > --
    > use Perl;
    > program
    > fulfillment
    >
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]
    David Guest

  4. #4

    Default RE: Reg Exp

    Hi David,

    Thanks for ur suggestion.

    But I am a new-bie to Perl. Can u give me a example for 'look-ahead'
    matches.

    Regards,
    Mallik.

    -----Original Message-----
    From: David le Blanc [mailto:com]
    Sent: Tuesday, October 05, 2004 5:57 PM
    To: John W. Krahn
    Cc: Perl Beginners
    Subject: Re: Reg Exp


    Your use of (.?)($del)? does not do what you expect. In the case of
    :-:a:-: you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
    :-::-: you will find (.?) = ":" and ($del)? matches nothing.

    Check out 'look-ahead' matches for a way to solve this.



    On Tue, 05 Oct 2004 04:03:21 -0700, John W. Krahn <net> wrote: 
    >
    > Hello,
    > [/ref]
    below [/ref]
    '::'s 
    >
    > Perhaps you need to use split and join:
    >
    > $a = ':-:m:-:a:-:l:-::-:k';
    >
    > my $del = ':-:'; # Delimeter
    >
    > $b = join $del, split $del, $a, -1;
    >
    > John
    > --
    > use Perl;
    > program
    > fulfillment
    >
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Mallik Guest

  5. #5

    Default RE: Reg Exp

    On Tue, 5 Oct 2004, Mallik wrote:
     

    30 seconds on Google returned *lots* of doentation.
    Here's some of it:

    <http://www.perldoc.com/perl5.8.4/pod/perlre.html>
    <http://www.perl.com/pub/a/2001/05/01/expressions.html>
    <http://www.regular-expressions.info/lookaround.html>

    If you *really* want to learn this stuff, get a copy of Jeffrey Friedl's
    _Mastering Regular Expressions_. It's a superb book that explains how
    regexes work in a variety of languages, including Perl.

    You should also get familiar with perldoc -- Perl's doentation
    system. You can look at a lot of perldoc doentation at perldoc.com,
    but this should also all exist on your computer as well. Try this:

    perldoc perlre

    Read over the doentation and let us know if you have more questions.


    --
    Chris Devers
    Chris Guest

  6. #6

    Default Re: Reg Exp

    On Tue, 05 Oct 2004 15:22:25 -0700, John W. Krahn <net> wrote: 
    > >
    > > Your use of (.?)($del)? does not do what you expect. In the case of
    > > :-:a:-: you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
    > > :-::-: you will find (.?) = ":" and ($del)? matches nothing.
    > >
    > > Check out 'look-ahead' matches for a way to solve this.[/ref]
    >
    > What do you mean? I am not using (.?)($del)? in my example.[/ref]

    Sorry I was replying to Malliks original. My emailer decided I was replying to
    the 'thread' and picked the last email in the thread to attach.

    I figured out how to fix this now :-)

    ciao
     
    David Guest

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