Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

proper RegEx to parse these strings

Posted on 2013-11-10
6
Medium Priority
?
380 Views
Last Modified: 2013-11-11
Hi,

I have a file with lines like this:

CBOEA11/08/13iShares MSCI Brazil Capped ETF1EWZ  Jan15201600047000  P 11/08/13
CBOEA11/08/13Cincinnati Financial Corp (EUR2CINF Jan14201400050000C   11/08/13
CBOEA11/08/13HONEYWELL INTL INC (NEW)      HON   Jan15201600077050C P 11/11/13
CBOEA11/08/13iShares iBoxx $ High Yield CorHYG   Jan15201600084000C P 11/11/13
CBOEA11/08/13Oil States International, Inc.OIS   Jan15201600100000C P 11/11/13

Open in new window

(note the even spacing).

I'm trying to extract several a couple of pieces of information from each line.
Stock Ticker (EWZ, CINF, HON, HYG, OIS)
Date (Jan152016, Jan142014 etc.)

I can do this in one or more steps, it doesn't matter.  My regex knowledge is limited, but I started off with this:

(Feb|Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|Cap){1}[0-9]*

I did r = regex.search(string), and r.groups() returns (u'Jan',)

Shouldn't this regex match the entire  number after "Jan"?  Of course with this date I need to match the 3 letter month and then grab the next 6 digits.

The ticker looks to be rather hard.  Ideas on that too?
Thanks!
0
Comment
Question by:ugeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 39637872
Perhaps try:
([A-Z]{3,4})\s*((?:Feb|Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|Cap){1}[0-9]{6})
I tested it here, and it seems to work ok.

I put a ?: into the brackets that determine the options for month, which makes them non-capturing, and added some extra brackets that capture the 6 digits following the month.

I've assumed the ticker is always caps, and is the last 3 or 4 characters before the date.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39637913
The groups method only lists out the captures of any capture groups that you specified in your pattern. However, when you do not explicitly specify which group you are interested in, it defaults to starting with group 1. Using the group method instead, it will default to the zeroth group, which is the entire match, regardless of how many capture groups there are.

e.g.

r.group()

Open in new window

0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39639838
Okay. Let's make the regex a tad simpler.

First, start out with what you're looking for [first]:

(ewz|hon|hyg|ois|cinf)

followed by what doesn't matter:

\s*

and by this point we know what's coming next, so just grab it:

(\w{3}\d{6})

Slap our case-insensitive flag on there, and we have a resulting regex of:

(?i)(ewz|hon|hyg|ois|cinf)\s*(\w{3}\d{6})
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 39639849
@bigdogdman, you're assuming those values are the only possible ticker codes, which is unlikely to be the case, given the nature of stock ticker data.
0
 
LVL 11

Author Closing Comment

by:ugeb
ID: 39639854
That works perfectly, thank you!
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39639865
@Terry,

Ah, indeed you are right. I must've misread the question. :-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

730 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