Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Pattern Match

by Athanasius (Archbishop)
on Jun 08, 2017 at 07:51 UTC ( [id://1192332]=note: print w/replies, xml ) Need Help??


in reply to Pattern Match

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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found