Professional Web Applications Themes

nested parens - PERL Beginners

Is there a module out there that I can use to p a text line and return the pieces that are enclosed in paren's? Thanks...

  1. #1

    Default nested parens

    Is there a module out there that I can use to p a text line and return the pieces that are enclosed in paren's?

    Thanks


    ewalker@micron.com Guest

  2. #2

    Default Re: nested parens

    com wrote: 

    Yep, a core module name Text::Balanced.

    Randy.
    Randy Guest

  3. #3

    Default RE: nested parens

    thanks


    -----Original Message-----
    From: Randy W. Sims [mailto:org]
    Sent: Friday, April 02, 2004 5:24 PM
    To: ewalker
    Cc: org
    Subject: Re: nested parens


    com wrote: 

    Yep, a core module name Text::Balanced.

    Randy.
    thanks
    ewalker@micron.com Guest

  4. #4

    Default Re: nested parens

    com wrote: 

    You don't need a module for that just use regex's:

    my text_inside_parens = $string =~ m/\((.*)\)/g;

    HTH

    Lee.M - JupiterHost.Net
     
    Jupiterhost.Net Guest

  5. #5

    Default RE: nested parens

    JupiterHost.Net <net> wrote:
    :
    : com wrote:
    : > Is there a module out there that I can use to p
    : > a text line and return the pieces that are enclosed
    : > in paren's?
    :
    : You don't need a module for that just use regex's:
    :
    : my text_inside_parens = $string =~ m/\((.*)\)/g;

    Let's test it.

    use strict;
    use warnings;
    use Data::Dumper 'Dumper';

    foreach my $string (
    '(foo) (bar)',
    '(foo) (bar) (baz)',
    '((foo) bar)) (baz)',
    '(foo bar)', ) {

    my text_inside_parens = $string =~ m/\((.*)\)/g;
    print Dumper \text_inside_parens;
    }

    __END__
    I get:

    $VAR1 = [
    'foo) (bar'
    ];
    $VAR1 = [
    'foo) (bar) (baz'
    ];
    $VAR1 = [
    '(foo) bar)) (baz'
    ];
    $VAR1 = [
    'foo bar'
    ];


    .* is greedy. I suspect text_inside_parens will
    never have more than one element in it.



    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

    Charles Guest

  6. #6

    Default Re: nested parens

    > Let's test it. 

    Right, The point was, regexes could help, I never said it wasn't greedy
    or the example was 100% what he needed, just a suggestion for another
    place to start looking :)
    (I figured it was a *little* better than `perldoc perlre` ;p)

    Thanks for taking the time to do that example.

    Lee.M - JupiterHost.Net
     
    Jupiterhost.Net Guest

  7. #7

    Default Re: nested parens

    "Charles K. Clarkson" wrote:
     
     

    I see. Someone dun fergot the lazy symbol, huh?
    ....
     

    good point.
    ....
    my text_inside_parens = $string =~ m/\((.*?)\)/g;
    print Dumper \text_inside_parens;
    }

    __END__
    $VAR1 = [
    'foo',
    'bar'
    ];
    $VAR1 = [
    'foo',
    'bar',
    'baz'
    ];
    ....

    Joseph

    R. Guest

  8. #8

    Default Re: nested parens

    >> Let's test it. 
    >
    >
    > Right, The point was, regexes could help, I never said it wasn't greedy
    > or the example was 100% what he needed, just a suggestion for another
    > place to start looking :)
    > (I figured it was a *little* better than `perldoc perlre` ;p)[/ref]

    To illustrate further try:
    perl -e 'my $str = "(hi)((bye)j(hi))(sweet)";my m = $str =~
    m/\(([^(^).]*)\)/g; for(m) { print "-$_-\n"; }'

    See? No module, lots of regex :) Again, maybe not exactly what they
    needed (IE It misses the middle 'j')but an illustration of an idea.

    And so everyone who is playing along at home:
    #!/usr/bin/perl

    use strict;
    use warnings;
    use Data::Dumper 'Dumper';

    foreach my $string (
    '(foo) (bar)',
    '(foo) (bar) (baz)',
    '((foo) bar)) (baz)',
    '(foo bar)', ) {

    my text_inside_parens = $string =~ m/\(([^(^).]*)\)/g;
    print Dumper \text_inside_parens;
    }

    HTH

    Lee.M - JupiterHost.Net
     
    >
    >[/ref]
    Jupiterhost.Net Guest

Similar Threads

  1. Nested XML into comboBox
    By leotemp in forum Macromedia Flex General Discussion
    Replies: 3
    Last Post: June 23rd, 10:59 PM
  2. Nested classes?
    By McRib64 in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: May 13th, 12:04 AM
  3. nested forms
    By Chris in forum Macromedia ColdFusion
    Replies: 5
    Last Post: July 6th, 08:15 AM
  4. Use of alternation with "selection" parens
    By Johnny Ingersoll in forum PERL Beginners
    Replies: 2
    Last Post: November 1st, 12:05 PM
  5. Nested datalist
    By Dino Novak in forum ASP.NET General
    Replies: 1
    Last Post: August 1st, 06:14 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