[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What does this regexp mean in Perl?

Posted on 2012-09-11
2
Medium Priority
?
338 Views
Last Modified: 2012-09-14
Can you please tell me what this regexp means?

.*?\.cpp

Open in new window


I use this to get the files with the extension .cpp

But I am wondering if I need to use ? in this regexp and if yes what it means?
0
Comment
Question by:Tolgar
2 Comments
 
LVL 28

Accepted Solution

by:
FishMonger earned 1000 total points
ID: 38387227
The . matches any character

The * is a quantifier meaning 0 or more of the preceding match and match as mush as possible (greedy)

The ? makes the preceding match non greedy, match the least amount

The \ escapes the . to match it literally

Mastering Regular Expressions, 3rd Edition
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 1000 total points
ID: 38387291
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr/.*?\.cpp/)->explain"
The regular expression:

(?-imsx:.*?\.cpp)

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):
----------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
----------------------------------------------------------------------
  \.                       '.'
----------------------------------------------------------------------
  cpp                      'cpp'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------


If you had a string like "abc.cppdef.cppghi" then
(.*?\.cpp) would match "abc.cpp"
while
(.*\.cpp) would match "abc.cppdef.cpp"
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
Six Sigma Control Plans
Suggested Courses

872 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