• 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?


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?
2 Solutions
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
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr/.*?\.cpp/)->explain"
The regular expression:


matches as follows:
NODE                     EXPLANATION
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
  .*?                      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"
(.*\.cpp) would match "abc.cppdef.cpp"
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