我的正则表达式需要帮助。
我需要删除文本开头的特殊字符
比如我有这样一段文字
.just a $#text this should not be incl#uded
输出应该是这样的
just a text this should not be incl#uded
I've been testing my regex here but i can't make it work
([\!-\/\;-\@]+)[\w\d]+
如何限制正则表达式只检查以特殊字符开头的文本?
谢谢
最佳答案
使用\B[!-/;-@]+\s*\b
:
var result = Regex.Replace(s, @"\B[!-/;-@]+\s*\b", "");
参见 regex demo
详情
\B
- 单词边界以外的位置(当前位置左侧必须有字符串开头或非单词字符)[!-/;-@]+
- 1 个或多个 ASCII 标点符号\s*
- 0+ 个空白字符\b
- 单词边界,当前位置的右边必须有一个字母/数字/下划线。
如果您打算删除所有标点符号,请使用
var result = Regex.Replace(s, @"\B[\p{P}\p{S}]+\s*\b", "");
参见 another regex demo .
请注意,\p{P}
匹配任何标点符号,\p{S}
匹配任何符号。
关于c# - 正则表达式替换特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808201/