473,320 Members | 1,896 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,320 software developers and data experts.

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 26276
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Craig Bailey | last post by:
Someone please explain what alternate universe I fell into this afternoon when PHP started telling me that 2 doesn't equal 2. Not sure about you, but when I run this, it tells me 59001.31 doesn't...
2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
5
by: Steve Wylie | last post by:
I am constructing an HTML questionnaire and one of the questions requires people to rate some choices from 1 to 5, where 1 is their favourite and 5 is their least favourite: Car Bus Taxi cab...
8
by: pw | last post by:
Hi, I need to create a function in javascript to check or uncheck all checkboxes in a form. From what I understand, I can do this either by specifying the name of the check box fields such as: ...
7
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
5
by: starke1120 | last post by:
Im creating a check in – check out database for RF guns. I have a table that contains models. ID (primary key) Model A table that contains Gun Details ID (primary key) Model_id...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.