Reputation: 543
I want to delete any numbers that have 3 or less than 3 digits. Can someone please help me with a regex that does this?
Currently, my code removes all the numbers it finds.
# Cleans Numbers
def cleanNumbers(stringToClean):
stringToClean = re.sub(r'[0-9]*', r'', stringToClean)
print 'String after cleaning : %s' %stringToClean
return stringToClean
Numbers will be surrounded by space. Example string I pass into the function :
connection breaks on Win8 client after a while. [persistence] 123 1 22 333 4444 554665 645fdgf45 ds3434 457870978934787843 345342kl
I call the above function as follows :
# Main function, calls other functions
def main():
# Parsing the input query
searchQuery = open('input.txt', 'r').read()
print 'Input query : %s' %searchQuery
# Cleaning the input query
string = CleanUpText.cleanNumbers(searchQuery)
Upvotes: 4
Views: 10115
Reputation: 19539
I have corrected the question, '3 or less than 3'
Given that, it should be as simple as: \b\d{1,3}\b
Upvotes: 1
Reputation: 1903
You could use a regex like this
r'\b[0-9]{1,2}\b'
Edit: Sorry wrote my answer to fast without really thinking. You have to use boundries so you don't capture 3456 for example
Upvotes: -1