Solved

regular expression

Posted on 2006-11-23
5
371 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
Comment Utility
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
Comment Utility
[^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
Comment Utility
"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
Comment Utility
discard the sequence
0
 
LVL 17

Expert Comment

by:ramrom
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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 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 …

771 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

12 Experts available now in Live!

Get 1:1 Help Now