Solved

regular expression

Posted on 2006-11-23
5
377 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 500 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

717 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