473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Remove some characters from a string

Hi,

I can't seem to find the right regular expression to achieve what I
want. I'd like to remove all characters from a string that are not
numbers, letters or underscores.

For example:
>>magic_function('si_98%u^d@.as-*gf')
str: 'si_98udasgf'

Would you have any hint?

Thanks a lot!

Julien
Jul 17 '08 #1
3 2326
On Jul 17, 10:13*am, Julien <jpha...@gmail.comwrote:
Hi,

I can't seem to find the right regular expression to achieve what I
want. I'd like to remove all characters from a string that are not
numbers, letters or underscores.

For example:
>magic_function('si_98%u^d@.as-*gf')

str: 'si_98udasgf'

Would you have any hint?

Thanks a lot!

Julien
One quick and dirty way would be...

import string
safe_chars = string.ascii_letters + string.digits + '_'
test_string = 'si_98%u^d@.as-*gf'
''.join([char if char in safe_chars else '' for char in test_string])

you could also use a translation table, see string.translate (the
table it uses can be made with string.maketrans)
Jul 17 '08 #2
On Jul 17, 9:13*am, Julien <jpha...@gmail.comwrote:
Hi,

I can't seem to find the right regular expression to achieve what I
want. I'd like to remove all characters from a string that are not
numbers, letters or underscores.

For example:
>magic_function('si_98%u^d@.as-*gf')

str: 'si_98udasgf'
For speed, you can use 'string.translate', but simplest is to use a
comprehension:

import string

def magic_function(s, keep=string.ascii_letters + string.digits +
'_'):
return ''.join(c for c in s if c in keep)

--
Paul Hankin
Jul 17 '08 #3
Julien wrote:
I can't seem to find the right regular expression to achieve what I
want. I'd like to remove all characters from a string that are not
numbers, letters or underscores.

For example:
>>>magic_function('si_98%u^d@.as-*gf')
str: 'si_98udasgf'
the easiest way is to replace the things you don't want with an empty
string:
>>re.sub("\W", "", "si_98%u^d@.as-*gf")
'si_98udasgf'

("\W" matches everything that is "not numbers, letters, or underscores",
where the alphabet defaults to ASCII. to include non-ASCII letters, add
"(?u)" in front of the expression, and pass in a Unicode string).

</F>

Jul 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joseph | last post by:
I have a textBox that people writes stories in it. They can use for format. I have Aspell installed on the server, so people can make correction to their text. Sometimes, they forget to add a...
11
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
9
by: Smiley | last post by:
Hi, Can someone tell me how to remove aspostophe (') from user input. I don't want to give any error message. Just want to remove or change it to "" or null, such input when it appear anywhere...
11
by: cody | last post by:
Is there a method to replace special characters like Ä (A-Umlaut) with A, Ö (O-Umlaut) with O, and so on? Sure, I could look for each character separately and replace it with its...
2
by: Konstantinos Pachopoulos | last post by:
Hi, i have the following string s and the following code, which doesn't successfully remove the "\", but sucessfully removes the "\\". .... if i!="\\": .... newS=newS+i .......
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
4
by: MC | last post by:
Is there a string function in .NET that will remove the accent marks from letters? I know that's a slightly vague request... and that I could implement it by table lookup (and will do so unless...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.