What does this regexp mean in Perl?

Posted on 2012-09-11
Last Modified: 2012-09-14
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?
Question by:Tolgar
    LVL 28

    Accepted Solution

    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
    LVL 84

    Assisted Solution

    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"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    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…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    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…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now