Solved

simple perl question

Posted on 2000-05-17
4
245 Views
Last Modified: 2012-08-13
hello all,
i have problem in understanding the following code:

 $text =~ s{ %% (.*?) %%}
    { exists( $fillings->{$1} ) ? $fillings->{$1} : "" }gsex;

can anyone explain it in very detail?

what is g?s?e?x? (i mean the gsex)

i guess it gets (.*?) and then
check if $fillings{$1} exists, if exist, substitute $1 by $fillings{$1},
if not, substitute $1 by "",
isn't it?

please explain in details...
thanks.
0
Comment
Question by:mwhuen
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 20 total points
Comment Utility
The following list defines Perl's pattern-matching operators.

g = Match globally, i.e., find all occurrences
s = Treat string as single line.
e = Evaluate the right side as an expression.
x = Use extended regular expressions.

the above pattern searches for all occurances of anything between %% %% and that string is passed to exists.

Exists function returns true if the specified hash key exists in its hash, even if the corresponding value is undefined.

if true is returned then $fillings->{$1} is evaluated else ""
0
 

Expert Comment

by:bighuen
Comment Utility
what is extended regular expressions?
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility

"what is extended regular expressions?"

Extended Regular Expressions

Perl defines an extended syntax for regular expressions. The syntax is a pair of parentheses with a question mark as the first
thing within the parentheses. The character after the question mark gives the function of the extension. The extensions are:

(?#text)

     A comment. The text is ignored.

(?:...)

     This groups things like "(...)" but doesn't make backreferences.

(?=...)

     A zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without
     including the tab in $&.

(?!...)

     A zero-width negative lookahead assertion. For example, /foo(?!bar)/ matches any occurrence of "foo" that isn't
     followed by "bar".

(?<=...)

     A zero-width positive lookbehind assertion. For example, /(?<=bad)boy/ matches the word boy that follows bad,
     without including bad in $&. This only works for fixed-width lookbehind.

(?<!=...)

     A zero-width negative lookbehind assertion. For example, /(?<!=bad)boy/ matches any occurrence of "boy" that
     doesn't follow "bad". This only works for fixed-width lookbehind.

(?>...)

     Matches the substring that the standalone pattern would match if anchored at the given position.

(?(condition)yes-pattern|no-pattern)
(?(condition)yes-pattern)

     Matches a pattern determined by a condition. The condition should be either an integer, which is "true" if the pair of
     parentheses corresponding to the integer has matched, or a lookahead, lookbehind, or evaluate, zero-width assertion.
     The no-pattern will be used to match if the condition was not meant, but it is also optional.



Hope that helps.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
perldoc perlre
                 x   Extend   your   pattern's   legibility  by  permitting
                     whitespace and comments.

                 These are usually written as  ``the  /x  modifier'',  even
                 though  the  delimiter in question might not actually be a
                 slash.  In fact,  any  of  these  modifiers  may  also  be
                 embedded  within  the  regular expression itself using the
                 new (?...) construct.  See below.

                 The /x modifier itself needs a  little  more  explanation.
                 It   tells   the   regular  expression  parser  to  ignore
                 whitespace  that  is  neither  backslashed  nor  within  a
                 character  class.   You  can  use  this  to  break up your
                 regular expression into (slightly)  more  readable  parts.
                 The  #  character  is  also  treated  as  a  metacharacter
                 introducing a comment, just  as  in  ordinary  Perl  code.
                 This  also  means  that  if  you want real whitespace or #
                 characters in the pattern (outside of a  character  class,
                 where  they are unaffected by /x), that you'll either have
                 to escape them or encode them using octal or hex  escapes.
                 Taken  together,  these  features  go  a  long way towards
                 making Perl's regular  expressions  more  readable.   Note
                 that  you  have  to  be careful not to include the pattern
                 delimiter in the commentperl has no way of knowing you did
                 not  intend to close the pattern early.  See the C-comment
                 deletion code in the perlop manpage.


(?x) syntax does not require a /x switch
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now