简体   繁体   English

正则表达式删除特定字符

[英]Regex expression to remove specific characters

I'm currently trying to get into regex expressions - at the moment I want to write one which acts as follows:我目前正在尝试使用正则表达式 - 目前我想编写一个如下所示的表达式:

import regex
a = '[[0.1 0.1 0.1 0.1]\n [1.2 1.2 1.2 1.2]\n [2.3 2.3 2.3 2.3]\n [3.4 3.4 3.4 3.4]]'
a_transformed = re.sub(regex_expression, a)

# a_transformed = '0.1 0.1 0.1 0.1 1.2 1.2 1.2 1.2 2.3 2.3 2.3 2.3 3.4 3.4 3.4 3.4'

Basically I only need to sub all occurences of (,n,[,]), but currently I'm struggling to get the expression right.基本上我只需要子 (,n,[,]) 的所有出现,但目前我正在努力让表达式正确。

Thanks for the help in advance!我在这里先向您的帮助表示感谢!

You can try the following:您可以尝试以下方法:

>>> re.sub(r'[^\d. ]', '', a)
'0.1 0.1 0.1 0.1 1.2 1.2 1.2 1.2 2.3 2.3 2.3 2.3 3.4 3.4 3.4 3.4'

Here '[^\d. ]'这里'[^\d. ]' '[^\d. ]' means anything except a digit, '.' '[^\d. ]'表示除数字之外的任何内容, '.' and space like characters.和空格一样的字符。 ^ inside [] means negate this character group. []内的^表示否定该字符组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM