Pattern Matching in Perl, regular expression

Posted on 2005-05-09
Last Modified: 2012-06-22

I have a list of information for security indices that looks like this:

^MERV MerVal Argentina 1,417.890 12:02PM ET  11.480 (0.80%) Components, Chart, More
^FTSE FTSE 100 United Kingdom 4,910.30 11:29AM ET  8.60 (0.17%) Chart, More

I am looking to retrieve just the ticker and the description in a new file, i.e.:
^MERV, MerVal Argentina
^FTSE, FTSE 100 United Kingdom

The number in the description (e.g. FTSE) is distinguishable from the other numbers because of the "," or the "."
so I have used multiple patterns:
      if($testLine =~ m/(^\S+)(.*)(\d+)(,|.)(.*)/)
            $testLine = "$1, $2\n";
            print TICKERSONLY $testLine;

but all seem to catch a value for $2 that is too greedy:
e.g.  MerVal Argentina 1,417.890 12:02PM ET

all I need is "Merval Argentina". What is wrong in my setup here ? Thanks for the help.

Question by:crogg01
    LVL 20

    Accepted Solution

    Try making the first instance of (.*) nongreedy by saying (.*?)

    Author Comment

    That did it! Thanks
    LVL 20

    Expert Comment

    You may find you have an extra space or tab at the end of your string.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    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…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    754 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

    17 Experts available now in Live!

    Get 1:1 Help Now