473,769 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6331
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|ma y|jun|jul|aug|s ep|nov|oct|dec' )
patr.match("j ul")
<_sre.SRE_Mat ch object at 0x7ff28ad8>
>>patr.match("n osuch")
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(calend ar.month_abbr[1:13])
pattern
'(?:Jan|Feb|Mar |Apr|May|Jun|Ju l|Aug|Sep|Oct|N ov|Dec)'
>>re.search(pat tern, "we are in september", re.IGNORECASE)
<_sre.SRE_Mat ch object at 0xb7ced640>
>>re.search(pat tern, "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(cal endar.month_abb r[1:13])
pattern
'(?:\\bJan|\\bF eb|\\bMar|\\bAp r|\\bMay|\\bJun |\\bJul|\\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|ma y|jun|jul|aug|s ep|nov|oct|dec' )
>>patr.match("j ul")
<_sre.SRE_Mat ch object at 0x7ff28ad8>
>>patr.match("n osuch")
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|ma y|jun|jul|aug|s ep|nov|oct|dec' )
patr.match("j ul")
<_sre.SRE_Matc h object at 0x7ff28ad8>
> >>patr.match("n osuch")

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|ma y|jun|jul|aug|s ep|nov|oct|dec' )
patr.match("j ul")
<_sre.SRE_Matc h object at 0x7ff28ad8>
>>patr.match("n osuch")

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(calend ar.month_name[1:13] +
calendar.month_ abbr[1:13])
>>pattern
'\\b(?:January| February|March| April|May|June| July|August|Sep tember|October| November|Decemb er|Jan|Feb|Mar| Apr|May|Jun|Jul |Aug|Sep|Oct|No v|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(pa ttern, target, re.IGNORECASE)
['august', 'sep']
>>re.search(pat tern, target, re.IGNORECASE)
<_sre.SRE_Mat ch object at 0xb7ced640>
>>re.findall(pa ttern, 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
4499
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 space (or comma) character as a delimiter. I can do one or the other, tokenize the words or tokenize the double-quoted strings, but I can't figure out how to combine the two into the same regex expression. Note: I *do* want to capture (retain)...
3
3093
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", "photo02.jpg");
2
1698
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*. If there's no function like that, if you have time and it is not so complex, can you help me code one? Thanks.
3
6032
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 sounds like making sense, but has anyone ever found this useful? Can this 'feature' be disabled? After having used StringTokenizer from the J-language that's not to be named, it's annoyed me for hours before I figured out that it was just a...
0
849
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 ends with different numbers each time. The text i want to append on is - &user=usertom. I know i use regex to do this. At the moment i have the text in a
3
20594
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
5922
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 right of '=' and left of '.' group3 contains right of '.' and left of '('...and so on. I'm not expert in using Regular expressions. Can some body help me how do I achieve this using Regex class?
2
1580
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 on which option is selected. Q2. Once the correct list is displayed, the user would then click (choose) from the list and enter a number (eg. hours worked) Q3. which must be totaled. any help would be appreciated
1
3349
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 second ip address. any help is great! im new wih regex
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.