Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

regular expression

Posted on 2006-11-23
5
Medium Priority
?
380 Views
Last Modified: 2010-04-16
i need a regular expression which will match sequence with aplhanumeric characters and containig chracters : , and ., if the sequence contain anything other than these chracters it will discard it
0
Comment
Question by:jyotishb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 18005103
Try this script to see if it is the answer to your question:

import re

rex = re.compile(r'[^a-zA-Z0-9:.]+')    # It is more efficient to compile the pattern
                                        # if the regular expression is to be used intensively.

s = 'abc 11. 5. 2006  14:35 <?+- ABC _' # the string to be processed
print s
s2 = rex.sub('', s)                     # the unwanted sequences erased
print s2
s3 = rex.sub(' ', s)                    # the unwanted sequences replaced by spaces
print s3
s4 = rex.sub('+++', s)                  # the unwanted sequences replaced by a chosen string
print s4

It will produce the followint output:

abc 11. 5. 2006  14:35 <?+- ABC _
abc11.5.200614:35ABC
abc 11. 5. 2006 14:35 ABC
abc+++11.+++5.+++2006+++14:35+++ABC+++


0
 
LVL 17

Accepted Solution

by:
ramrom earned 1500 total points
ID: 18005109
[^a-zA-Z0-9:,.] used with search will return a match object if any character not in a-zA-Z0-9:,. is in the sequence.
0
 
LVL 17

Expert Comment

by:ramrom
ID: 18005114
"if the sequence contain anything other than these chracters it will discard it" discard the offending character or discard the sequence?
0
 

Author Comment

by:jyotishb
ID: 18005391
discard the sequence
0
 
LVL 17

Expert Comment

by:ramrom
ID: 18005593
OK then you want something like:

if re.search("[^a-zA-Z0-9:,.]", sequence): discard sequence

Also, as pepr points out you may compile the pattern first.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question