473,398 Members | 2,403 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,398 software developers and data experts.

regex matching

if i have a string, let's say its

xx = 'abc123def456ghi"

i'm having problems figuring out a way to know if xx[0] is a letter or number, then check xx[1] for the same thing and so on and so forth.

what i thought would be easiest is doing somethign through regex
have
pattern1 = re.compile('[a-z]')
pattern2 = re.compile('[0-9]')

can i make a loop that check something like...
Expand|Select|Wrap|Line Numbers
  1. j = 0
  2. while j < len(xx):
  3.     if j == pattern1:
  4.         print "its a letter"
  5.  
Jan 26 '08 #1
2 1153
bvdet
2,851 Expert Mod 2GB
if i have a string, let's say its

xx = 'abc123def456ghi"

i'm having problems figuring out a way to know if xx[0] is a letter or number, then check xx[1] for the same thing and so on and so forth.

what i thought would be easiest is doing somethign through regex
have
pattern1 = re.compile('[a-z]')
pattern2 = re.compile('[0-9]')

can i make a loop that check something like...
Expand|Select|Wrap|Line Numbers
  1. j = 0
  2. while j < len(xx):
  3.     if j == pattern1:
  4.         print "its a letter"
  5.  
You can also do something like this:
Expand|Select|Wrap|Line Numbers
  1. import string
  2.  
  3. xx = 'abc123def456ghi>'
  4.  
  5. for letter in xx:
  6.     if letter in string.ascii_letters:
  7.         print '"%s" is a letter' % letter
  8.     elif letter in string.digits:
  9.         print '"%s" is a digit' % letter
  10.     else:
  11.         print '"%s" is neither a letter or a digit' % letter
Jan 27 '08 #2
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. >>> map(str.isalpha,xx)
  2. [True, True, True, False, False, False, True, True, True, False, False, False, True, True, True]
  3. >>> zip(xx,map(str.isalpha,xx))
  4. [('a', True), ('b', True), ('c', True), ('1', False), ('2', False), ('3', False), ('d', True), ('e', True), ('f', True), ('4', False), ('5', False), ('6', False), ('g', True), ('h', True), ('i', True)]
  5. >>>                          
  6.  
Jan 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: aeuglein | last post by:
Hello! I have this RegEx: /(+:\/\/+)/i Now, I want to exlude on the end of a String the formats .gif / .jpg / ..png / .exe / .zip / .rar How I can this add to my regex ?
2
by: mikea59 | last post by:
I am getting errors in XMLSpy (Pro) in the following case: Source Document: <test> 12345 AB 12345 </test> Stylesheet: <xsl:stylesheet version="2.0"...
3
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
5
by: Kofi | last post by:
Any takers? Got a string of DNA as an input sequence GGATGGATG, apply the simple regex "GGATG" as in Regex r = new Regex("GGATG", (RegexOptions.Compiled)); MatchCollection matches =...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
7
by: CB | last post by:
Trying to match the entire following object literal code using a RegEx. var Punctuators = { '{' : 'LeftCurly', '}' : 'RightCurly' } Variations on the idea of using /var.*{.*}/ of course stops...
0
by: Tidane | last post by:
Visual Basic.NET Framework 2.0 I've created a program to parse out text as the program recieved it and use Regex matching to decide what should be done. My problem is that the text is matching when...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.