473,549 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26323
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('1 23123[a-z]adsfasfd').grou p() '[a-z]'

Regex pattern: import re
regex = re.compile('[a-z]')
regex.search('1 23123123a123123 b123123c').grou p() 'a' regex.findall(' 123123123a12312 3b123123c') ['a', 'b', 'c']

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

['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('1 23123123132'): 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(' 123a23423b13123 c123123A123B123 C')

['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.rp i.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**@bogusaddr ess.xyz> wrote:
Also, how would I check if [a-z] is not in a string?
import string

sometext = "whatever"
if not string.lowercas e 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.lowercas e in sometext:
[A-Z]
if string.uppercas e 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|u pper)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******@chell o.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.lowercas e[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.lowercas e in sometext:
<do what you wanted to do>


Let's see:
import string
sometext = "whatever"
if not string.lowercas e 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|u pper)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.lowercas e/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.lowercas e in sometext:
<do what you wanted to do>


Let's see:
import string
sometext = "whatever"
if not string.lowercas e 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|u pper)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.lowercas e/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

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

Similar topics

17
4165
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 equal 59001.31. Change each side of the equation by a hundreth or two and it checks. Change it a bit more, and it won't. _What_ is going on...
2
3207
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
2423
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> <body> <form name="myform" action=test.php method=post>
5
6303
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 Train Airplane
8
2471
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: function checkAll(list) { var o = document.getElementById(list); var c = o.checked; var f = document.forms.f; var a = f.item(list);
7
29869
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 *** Don't just participate in USENET...get rewarded for it!
2
3583
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 check/uncheck it. Also, what event do I use to check it for a value? Any articles out there? -- Chris Davoli
16
5640
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
11642
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 Gun_Number (assigned gun number by operations)
1
2400
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. I want to display these results in check boxes. This is my code. But it doesn't work <tr><td><input value="1" type="checkbox" id="1" name="check"...
0
7541
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7464
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...
0
7734
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. ...
0
7826
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5107
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...
0
3512
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
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...

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.