Solved

Meaning of  "(.*)\"  in a pattern.

Posted on 2007-11-20
5
284 Views
Last Modified: 2010-04-21
What do  "(.*)\"  do in the pattern below?


elsif ( $line =~ m/^\|\s+AT\_name\s+\"(.*)\"/ )
{


}
0
Comment
Question by:naseeam
  • 2
  • 2
5 Comments
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 240 total points
ID: 20324012
The parentheses save whatever is matched inside them to $1
The .* matches any character 0 or more times, as many as possible
the \" matches a double-quote, and the \ is not necessary

So, if $line was '| AT_name "something in here"', then $1 would be "something in here"
0
 
LVL 84

Accepted Solution

by:
ozo earned 260 total points
ID: 20324203
perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/^\|\s+AT\_name\s+\"(.*)\"/)->explain'
The regular expression:

(?-imsx:^\|\s+AT_name\s+"(.*)")

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \|                       '|'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  AT_name                  'AT_name'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
 

Author Comment

by:naseeam
ID: 20324277
Is $1 perl macro?
0
 
LVL 84

Expert Comment

by:ozo
ID: 20324350
$1 is a perl variable
perldoc perlvar
       $<digits>
               Contains the subpattern from the corresponding set of capturing
               parentheses from the last pattern match, not counting patterns
               matched in nested blocks that have been exited already.
               (Mnemonic: like \digits.)  These variables are all read-only
               and dynamically scoped to the current BLOCK.
0
 

Author Closing Comment

by:naseeam
ID: 31410248
Nice details.  Quick responses.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

9 Experts available now in Live!

Get 1:1 Help Now