Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Pattern Match

by Nansh (Acolyte)
on Jun 08, 2017 at 06:06 UTC ( [id://1192327]=perlquestion: print w/replies, xml ) Need Help??

Nansh has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a problem with pattern matching My file looks like below:

Fruits{ Apple Mango Grape Watermelon }

My another file looks like this

Fruit{ Apple }

My code works for both of the files here thing is we shouldn't use .* that affects my other code. so can anyone help me to solve this? logical part for this is below  $_=~ m/^\s*Fruits\s*\{\s*(.*)?\s*\}/; Thing is I should not use .* so is there any other way to match that?, If so please help me. Thank you.

Replies are listed 'Best First'.
Re: Pattern Match
by Ratazong (Monsignor) on Jun 08, 2017 at 06:21 UTC

    Dear Nansh,

    the main reason why the dotstar is considered evil: it often doesn't do what you want. In your example it would also match Fruits{ }{ }. You can see a detailed discussion in Ovids famous node Death to Dot Star!. Of course if you know exactly what you do, the dotstar is a powerful weapon.

    Not using the dotstar typically forces you to think more on your expected content. In you case, you are expecting letters and (probably) blanks as separators. So you could simply look up the shortcodes for them, e.g. in the documentation.

    HTH, Rata

Re: Pattern Match
by Athanasius (Archbishop) on Jun 08, 2017 at 07:51 UTC

    Hello Nansh,

    My code works for both of the files

    This can’t be exactly true, since a regex containing Fruits (note the literal character s) will never match Fruit{ Apple } in which the s is missing. Perhaps this was merely a typo; otherwise, you need to make the s optional: Fruits?.

    Speaking of ?, in its current position in the regex — (.*)? — it acts as a quantifier specifying zero or one repetitions of the preceding expression. In the present case this adds nothing, since the expression it quantifies already specifies zero or more characters. More likely, you were looking to make the * quantifier non-greedy, in which case the ? needs to go inside the capturing parentheses:

    use strict; use warnings; while (<DATA>) { print ">$1<\n" if / ^ \s* Fruits? \s* \{ \s* (.*?) \s* \} /x; } __DATA__ Fruits{ Apple Mango Grape Watermelon } Fruit{ Apple } Fruits{ Lemon Mandarin }{ Orange Fig Pineapple } Vegetable{ Carrot }

    Output:

    17:48 >perl 1784_SoPW.pl >Apple Mango Grape Watermelon< >Apple< >Lemon Mandarin< 17:48 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Pattern Match
by haukex (Archbishop) on Jun 08, 2017 at 07:02 UTC

    A simple replacement for .* is often something like [^}]*, that is, any characters except the closing one. However, this won't match nested brackets or other more complicated cases. For this, there are modules like Regexp::Common::balanced or perhaps Text::Balanced.

Re: Pattern Match
by hippo (Bishop) on Jun 08, 2017 at 07:58 UTC

    Since you haven't said where the problem actually lies for you the specification is incomplete. Maybe a read of How to ask better questions using Test::More and sample data will help with that. However, it is pretty trivial to avoid the dotstar in this case.

    #!/usr/bin/env perl use strict; use warnings; use Test::More; my @good = ( 'Fruits{ Apple Mango Grape Watermelon }', 'Fruit{ Apple }' ); my @bad = ( 'who', 'knows{ what }', 'you expect here' ); my $re = qr/^Fruits?{ [A-Za-z ]* }$/; plan tests => @good + @bad; for my $str (@good) { like ($str, $re, "$str matched"); } for my $str (@bad) { unlike ($str, $re, "$str not matched"); }
Re: Pattern Match
by tybalt89 (Monsignor) on Jun 08, 2017 at 10:24 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1192327 use strict; use warnings; print /^Fruit(?:\{|s\{(?: \w+)+) \w+ \}$/ ? 'Pass ' : 'Fail ', $_ while <DATA>; __DATA__ Fruits{ Apple Mango Grape Watermelon } Fruit{ Apple } Fruits{ Lemon Mandarin }{ Orange Fig Pineapple } Vegetable{ Carrot } who knows{ what } you expect here

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1192327]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-05-27 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found