开发者

Regular expression to remove some special chars

开发者 https://www.devze.com 2023-03-05 10:30 出处:网络
I\'m looking for a regular expression that can remove all the following c开发者_JAVA技巧haracters from a string (and Whitespace too):

I'm looking for a regular expression that can remove all the following c开发者_JAVA技巧haracters from a string (and Whitespace too):

~ % & \ ; : " ' , < > ? #

Can you help me? I'm coding in ActionScript 3.


In ActionScript it goes like

yourString.replace(/[~%&\\;:"',<>?#\s]/g,"");

Same in Perl:

$_ = "~ % & \\ ; : \" ' , < > ? #";
s/[~%&\\;:"',<>?#\s]//g;
print; #prints nothing


[~%&\\;:"',<>?#\s]+


removes all whitespaces in $mystring using perl, you will need to add the other chars to in order to create your regex

$mystring = "...";
$mystring =~ s/\s//g;


import re 
with open ('txt.txt', 'r') as fi: # to read ur file 
    tm = fi.read().replace(',', '\n,') # to replace " , " with \n{enter} i.e to new line
    with open ('out_3.txt','w') as z: # to crate new file wher all changes will done or we simply say that to write a new file 
        for j in tm:
            y = re.sub("[0-9 {} <> -,: _\. = +| \/]",'\n' , j)# to remove required 

regular expression u can change or modified as per requirement as I don't add "?" as regular expression but u can add as -- y = re.sub("[0-9 {} <> -,: _\.? = +| \/]",'\n' , j) for more details about re.sub u can read this documentation https://docs.python.org/3.1/library/re.html

for gn in y:
    z.write(gn)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号