Go Premium for a chance to win a PS4. Enter to Win


simple perl question

Posted on 2000-05-17
Medium Priority
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...
Question by:mwhuen
  • 2
LVL 16

Accepted Solution

maneshr earned 80 total points
ID: 2817950
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 ""

Expert Comment

ID: 2824738
what is extended regular expressions?
LVL 16

Expert Comment

ID: 2825702

"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:


     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.


     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.
LVL 85

Expert Comment

ID: 2826473
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

916 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