用于匹配特殊字符的正则表达式没有空格或换行符
RegEx for matching special chars no spaces or newlines
我有一个字符串,想使用正则表达式匹配所有字符,但没有空格。
我尝试将所有空格替换为空,使用:
Regex.Replace(seller, @"[A-z](.+)", m => m.Groups[1].Value);
//rating
var betyg = Regex.Replace(seller, @"[A-z](.+)", m => m.Groups[1].Value);`
我期待
的输出
"Iris-presenter | 5"
但是,输出是
"Iris-presenter"
在这个也看到在这个demo.
字符串是:
<spaces>Iris-presenter
<spaces>|
<spaces>5
好问题!我不太确定,如果这就是您要找的东西。 This expression 但是匹配您的输入字符串:
^((?!\s|\n).)*
图表
图表显示了它的工作原理:
编辑
根据revo的建议,表达式可以大大简化,因为
^((?!\s|\n).)*
is equal to ^((?!\s).)*
and both are equal to ^\S*
.
我用 (\s(.*?))
让它工作。这将删除看到的所有空格和新行 here
我有一个字符串,想使用正则表达式匹配所有字符,但没有空格。
我尝试将所有空格替换为空,使用:
Regex.Replace(seller, @"[A-z](.+)", m => m.Groups[1].Value);
//rating
var betyg = Regex.Replace(seller, @"[A-z](.+)", m => m.Groups[1].Value);`
我期待
的输出"Iris-presenter | 5"
但是,输出是
"Iris-presenter"
在这个也看到在这个demo.
字符串是:
<spaces>Iris-presenter
<spaces>|
<spaces>5
好问题!我不太确定,如果这就是您要找的东西。 This expression 但是匹配您的输入字符串:
^((?!\s|\n).)*
图表
图表显示了它的工作原理:
编辑
根据revo的建议,表达式可以大大简化,因为
^((?!\s|\n).)*
is equal to^((?!\s).)*
and both are equal to^\S*
.
我用 (\s(.*?))
让它工作。这将删除看到的所有空格和新行 here