469,338 Members | 8,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

How To Check For [a-z] In String?

This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Thanks,
Hank

Jul 18 '05 #1
10 23948
Also, how would I check if [a-z] is not in a string?

Thanks again,
Hank

Hank Kingwood wrote:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Thanks,
Hank


Jul 18 '05 #2


Hank Kingwood wrote:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?


Are you looking for the actual string [a-z] in a string, or are you
looking for the regular expression [a-z] (any one lowercase letter)

Actual string:
import re
regex = re.compile('\[a-z\]')
regex.search('123123[a-z]adsfasfd').group() '[a-z]'

Regex pattern: import re
regex = re.compile('[a-z]')
regex.search('123123123a123123b123123c').group() 'a' regex.findall('123123123a123123b123123c') ['a', 'b', 'c']

You could also do: import re
regex = re.compile('[a-z]|[A-Z]')
regex.findall('123a23423b13123c123123A123B123C')

['a', 'b', 'c', 'A', 'B', 'C']

There are probably other ways to do it with list comprehensions, but
does that help?

Jay

Jul 18 '05 #3
Hank Kingwood wrote:
Also, how would I check if [a-z] is not in a string?


following my re examples:
import re
regex = re.compile('[^a-z]')
if regex.search('123123123132'): print "no lower letters!"

....
no lower letters!

There is a good regular expression tutorial on
http://www.amk.ca/python/howto/regex/

jay
Jul 18 '05 #4
Jay Dorsey wrote:
>>> regex = re.compile('[a-z]|[A-Z]')
>>> regex.findall('123a23423b13123c123123A123B123C')

['a', 'b', 'c', 'A', 'B', 'C']


Line 1 above should have been [a-zA-Z] (although as posted it works as
well). Its just prettier the second way :-)

Jay
Jul 18 '05 #5
At 08:48 AM 10/2/2003, Hank Kingwood wrote:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would help
out, I'd appreciate it!

Also, how would you recommend searching for the following in the same string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array. Ideas?


Sounds like a perfect application for regular expressions. Check out the re
module. Example:
import re
re.findall(r'[a-zA-Z0-9]*', 'This is fun')

['This', '', 'is', '', 'fun', '']

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

Jul 18 '05 #6
On Thu, 02 Oct 2003 14:56:10 GMT, Hank Kingwood
<ha**@bogusaddress.xyz> wrote:
Also, how would I check if [a-z] is not in a string?
import string

sometext = "whatever"
if not string.lowercase in sometext:
<do what you wanted to do>

Thanks again,
Hank

Hank Kingwood wrote:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
if string.lowercase in sometext:
[A-Z]
if string.uppercase in sometext: ...
[0-9]
if string.digits in sometext: ...
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Thanks,
Hank


As others have shown, you can of course do this with regular
expressions, but the string module is your friend in this case, and
makes for very readable code, which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.
Just do a dir(string) to see what other goodies there are...

--
Christopher
Jul 18 '05 #7
On Thu, 02 Oct 2003 16:13:59 GMT, myself <kl******@chello.at> wrote:

which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.


Ouch, I take it back (the unicode thing), it's just all the
lower/uppercase letters from the latin-1 set, but not in unicode...

Also, if you want to check for ONLY [a-z], you can check for
string.lowercase[0:26]...

hasty, hasty, hasty...
--
Christopher
Jul 18 '05 #8
Christopher Koppler wrote:
Also, how would I check if [a-z] is not in a string?
import string

sometext = "whatever"
if not string.lowercase in sometext:
<do what you wanted to do>


Let's see:
import string
sometext = "whatever"
if not string.lowercase in sometext:

.... print "do what you wanted to"
....
do what you wanted to

I doubt that this is what you expected.

s1 in s2

tests if s1 is a substring of s2, but you want

def contains(s, chars):
for c in s:
if c in chars:
return True
return False
expressions, but the string module is your friend in this case, and
makes for very readable code, which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.
Just do a dir(string) to see what other goodies there are...


Most of these are already available as methods of the str class and are
duplicated here only for backwards compatibility.

What's actually in string.lowercase/uppercase depends on the locale, you
should by no means take latin-1 for granted.

You have already withdrawn the unicode-ready claim.

Nasty, nasty, nasty :-)

Peter
Jul 18 '05 #9
On Thu, 02 Oct 2003 19:01:27 +0200, Peter Otten <__*******@web.de>
wrote:
Christopher Koppler wrote:
Also, how would I check if [a-z] is not in a string?


import string

sometext = "whatever"
if not string.lowercase in sometext:
<do what you wanted to do>


Let's see:
import string
sometext = "whatever"
if not string.lowercase in sometext:

... print "do what you wanted to"
...
do what you wanted to

I doubt that this is what you expected.

s1 in s2

tests if s1 is a substring of s2, but you want

def contains(s, chars):
for c in s:
if c in chars:
return True
return False
expressions, but the string module is your friend in this case, and
makes for very readable code, which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.
Just do a dir(string) to see what other goodies there are...


Most of these are already available as methods of the str class and are
duplicated here only for backwards compatibility.

What's actually in string.lowercase/uppercase depends on the locale, you
should by no means take latin-1 for granted.

You have already withdrawn the unicode-ready claim.

Nasty, nasty, nasty :-)

Peter


<High embarassement mode>
Big oops. Yeah, that goes to show what happens if you multitask
yourself and don't let your computer do it - I'm preparing for the
second of the two Oracle Certified Associate exams, which I take
tomorrow, and thought I'd take some time out and maybe answer some
questions here... Seems I'm a bit too confused and disorganized to do
that right now...
</HEM>

--
Christopher
Jul 18 '05 #10
Hank Kingwood <ha**@bogusaddress.xyz> wrote in message news:<e0******************@newssvr12.news.prodigy. com>...
Also, how would I check if [a-z] is not in a string?

Thanks again,
Hank

Hank Kingwood wrote:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Thanks,
Hank

Use regular expressions.
import re
any_string="find a-z, A-Z and 0-9, **+#-#?** no specials"
re_search=re.compile(r'(\w+)')
re_search.findall(any_string) ['find', 'a', 'z', 'A', 'Z', 'and', '0', '9', 'no', 'specials'] # Or
re_search=re.compile(r'(\w)')
re_search.findall(any_string) ['f', 'i', 'n', 'd', 'a', 'z', 'A', 'Z', 'a', 'n', 'd', '0', '9', 'n',
'o', 's', 'p', 'e', 'c', 'i', 'a', 'l', 's']


The **\w** means a-z plus A-Z plus 0-9 plus _.
If you doen't want to get the underline you have to define:
[a-zA-Z0-9] instead.

For more information read the docu for the re-module.

Regards
Peter
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Craig Bailey | last post: by
2 posts views Thread by Askari | last post: by
2 posts views Thread by Edward | last post: by
5 posts views Thread by Steve Wylie | last post: by
8 posts views Thread by pw | last post: by
7 posts views Thread by Tony Johnson | last post: by
2 posts views Thread by Chris Davoli | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.