473,226 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

regex with specific list of string

hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'

Thanks
james

Sep 26 '07 #1
8 6310
On Wed, 2007-09-26 at 15:42 +0000, james_027 wrote:
hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'
Why regex? You can simply check if the given value is contained in the
set of allowed values:
>>s = set(['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'])
>>'jan' in s
True
>>'spam' in s
False

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 26 '07 #2
james_027 wrote:
hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'

Thanks
>>patr = re.compile('jan|feb|mar|apr|may|jun|jul|aug|sep|no v|oct|dec')
patr.match("jul")
<_sre.SRE_Match object at 0x7ff28ad8>
>>patr.match("nosuch")
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 26 '07 #3
Carsten Haese wrote:
On Wed, 2007-09-26 at 15:42 +0000, james_027 wrote:
>hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'

Why regex? You can simply check if the given value is contained in the
set of allowed values:

>>>s = set(['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'])
>>>'jan' in s
Also, check calendar for a locale aware (vs hardcoded) version:
>>import calendar
[calendar.month_abbr[i].lower() for i in range(1,13)]
['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']

If you still want to use regexes, you can do something like:
>>import re
pattern = '(?:%s)' % '|'.join(calendar.month_abbr[1:13])
pattern
'(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|De c)'
>>re.search(pattern, "we are in september", re.IGNORECASE)
<_sre.SRE_Match object at 0xb7ced640>
>>re.search(pattern, "we are in september", re.IGNORECASE).group()
'sep'

If you want to make sure that the month name begins a word, use the following pattern instead:
>>pattern = r'(?:\b%s)' % r'|\b'.join(calendar.month_abbr[1:13])
pattern
'(?:\\bJan|\\bFeb|\\bMar|\\bApr|\\bMay|\\bJun|\\bJ ul|\\bAug|\\bSep|\\bOct|\\bNov|\\bDec)'

If in doubt, Google for "regular expressions in python" or go to http://docs.python.org/lib/module-re.html
Regards,
Pablo

Sep 26 '07 #4
On Wed, 2007-09-26 at 12:49 -0400, Steve Holden wrote:
james_027 wrote:
hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'

Thanks
>>patr = re.compile('jan|feb|mar|apr|may|jun|jul|aug|sep|no v|oct|dec')
>>patr.match("jul")
<_sre.SRE_Match object at 0x7ff28ad8>
>>patr.match("nosuch")
Unfortunately, that also matches margarine, mayonnaise, and octopus,
just to name a few ;-)

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 26 '07 #5
Carsten Haese wrote:
On Wed, 2007-09-26 at 12:49 -0400, Steve Holden wrote:
>james_027 wrote:
>>hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'

Thanks
patr = re.compile('jan|feb|mar|apr|may|jun|jul|aug|sep|no v|oct|dec')
patr.match("jul")
<_sre.SRE_Match object at 0x7ff28ad8>
> >>patr.match("nosuch")

Unfortunately, that also matches margarine, mayonnaise, and octopus,
just to name a few ;-)
Indeed, but I think the essential point was served. Unlike the
mayonnaise and octopus.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 26 '07 #6
Carsten Haese wrote:
On Wed, 2007-09-26 at 12:49 -0400, Steve Holden wrote:
>james_027 wrote:
>>hi,

how do I regex that could check on any of the value that match any one
of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'

Thanks
patr = re.compile('jan|feb|mar|apr|may|jun|jul|aug|sep|no v|oct|dec')
patr.match("jul")
<_sre.SRE_Match object at 0x7ff28ad8>
>>patr.match("nosuch")

Unfortunately, that also matches margarine, mayonnaise, and octopus,
just to name a few ;-)
(and so does the solution you sent before :)

This is fine IMO since the OP didn't specify the opposite.

BTW in my previous post I included an example that ensures that the
search month matches the beginning of a word. That was based in that
maybe he wanted to match e.g. "dec" against "December" (BTW, it should
have been r'\b(?:Jan|Feb|...)' instead). To always match a whole word, a
trailing \b can be added to the pattern OR (much better) if the month
can appear both in its abbreviated and full form, he can use the
extensive set as follows (I hope this is clear, excuse my Thunderbird...):
>>pattern = r"\b(?:%s)\b" % '|'.join(calendar.month_name[1:13] +
calendar.month_abbr[1:13])
>>pattern
'\\b(?:January|February|March|April|May|June|July| August|September|October|November|December|Jan|Feb |Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\b'
>>target = "Unlike Julia, I like apricots with mayo in august or sep"
target
'Unlike Julia, I like apricots with mayo in august or sep'
>>re.findall(pattern, target, re.IGNORECASE)
['august', 'sep']
>>re.search(pattern, target, re.IGNORECASE)
<_sre.SRE_Match object at 0xb7ced640>
>>re.findall(pattern, target, re.IGNORECASE)
['august', 'sep']
Regards,
Pablo
Sep 26 '07 #7
Hi all,
This is fine IMO since the OP didn't specify the opposite.
Thanks for all your replies, though I don't understand quite well the
going argument? What do you mean when you say "the OP didn't specify
the opposite"?

There reason for using regex is because I am going to use it in
Django's URL pattern

Thanks
james

Sep 26 '07 #8
james_027 wrote:
Hi all,
>This is fine IMO since the OP didn't specify the opposite.

Thanks for all your replies, though I don't understand quite well the
going argument? What do you mean when you say "the OP didn't specify
the opposite"?

There reason for using regex is because I am going to use it in
Django's URL pattern
Carsten was pointing out that the pattern I gave you would match any
string that *began* with one of the month names, as I didn't include an
element to force a match of the end of the string.

I did this because I assumed you were most interested in finding out how
to match one of a number of alternate strings, and this would likely
only be a part of your final pattern.

If you already have what you need you really don't need to pay much
attention to the rest: it's just geeks picking nits!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 26 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert Oschler | last post by:
Can someone give me a regex expression that will split a sentence containing words and double-quoted phrases, into an array? I don't want the words between the double-quotes to be split using the...
3
by: Timmy | last post by:
I'm working on a simple click-through image gallery and I have images with captions in two arrays like this: var current_value="0"; var images = new Array ("photo01.jpg", "photo02.jpg",...
2
by: FrzzMan | last post by:
Hello, Is there any function that automatically escape all regex operator in a string? So that the string that contain regex operator will be identified as a *string*, not a *regex pattern*. ...
3
by: Rico | last post by:
If there are consecutive occurrences of characters from the given delimiter, String.Split() and Regex.Split() produce an empty string as the token that's between such consecutive occurrences. It...
0
by: kieran | last post by:
Hi, I need to change a bunch of links...i need to append a username on the end of each one. The link always starts the same i.e. - <a href=http://www.link.ie/kjlasdfl - but the rest of the link...
3
by: slg | last post by:
How can i validate the characters in a string are all hex chars. I tried following but it does not work. Regex r = new Regex(@"^(||)*"); TIA.
2
by: KK | last post by:
Dear All I have a string like this: myOutput = myObject.MyMethod(myInput1,myInput2) I would like to parse this string and separate it into 4 groups. group1 contains left of '='. group2 contains...
2
by: =?Utf-8?B?REFSVDQz?= | last post by:
Hi ... new Access user. I have a need to display a different list based on the selection from a dropdown list. eg. dropdown options ... A,B, or C Q1. I want a different list displayed depending...
1
by: raids51 | last post by:
im using some regex code in vb.net to get each ip address in a list that looks like this: ip,ip,ip,ip,ip,ip,ip,ip,ip,ip etc. but when i use this regex code ( |,).+?(,|\r\n) it only returns every...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.