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
Solved

proper RegEx to parse these strings

Posted on 2013-11-10
6
373 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
6 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

840 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