Solved

regular expression

Posted on 2006-11-23
5
374 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
  • 3
5 Comments
 
LVL 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now