问题描述
我一直在试图建立一个正规的前pression但一直没能得到一个特定的条件下工作。
I have been trying to build a regular expression but haven't been able to get one specific condition to work.
我希望有一个正则表达式删除所有非字母字符,除破折号( -
)。破折号应该只有当他们都用空格pfixed $ P $所取代。
I want a regex to remove all non alpha characters with the exception of dash (-
). Dashes should only be replaced if they are prefixed by a space.
即
TEST-TEST -TEST#TEST.TEST
应改为
TEST-TEST TEST TEST TEST
我一直在使用 [^ A-ZA-Z0-9]
但一直没能包括一个或条件初始化。
I had been using [^a-zA-Z0-9]
but haven't been able to include one OR condition init.
推荐答案
下面是我想出了(\ S-| [^ A-ZA-Z0-9 - ])
...这将删除所有非字母数字,但保留 - 除非有收到了空间 -
Here is what I came up with (\s-|[^A-Za-z0-9-])
... It will remove all non alphanumerics but keep the "-" except if there is a space before it " -"
测试使用sed在Linux中,此刻我没有访问VS或单声道,以测试在C#
Test using sed in Linux, at the moment I don't have access to VS or Mono to test in C#
echo "TEST-TEST -TEST#TEST.TEST -1234" | sed 's/\(\s-\|[^A-Za-z0-9-]\)/ /g'
输出
TEST-TEST TEST TEST TEST 1234
- ()和|用于OR条件 使用
- 我们首先删除所有 -
- 接下来我们把所有字母数字和 - 与
[^ A-ZA-Z0-9 - ]
- () and | are used for the OR condition
- We first remove all " -" using
\s-
- next we keep all alphanumerics and "-" with
[^A-Za-z0-9-]
\ S -
这篇关于普通防爆pression更换用空格非字母字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!