• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

What does this regexp mean in Perl?

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
Tolgar
Asked:
Tolgar
2 Solutions
 
FishMongerCommented:
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
 
ozoCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now