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
Solved

regular expression

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python output problem 10 54
Python - ImportError: No module named 'urllib2' 2 305
How does this Python sort work? 5 97
error with datetime python 1 20
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…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 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 …

860 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