正则表达式匹配字母数字和空格。

时间:2021-05-02 16:51:43

What am I doing wrong here?

我在这里做错了什么?

string q = "john s!";
string clean = Regex.Replace(q, @"([^a-zA-Z0-9]|^\s)", string.Empty);
// clean == "johns". I want "john s";

7 个解决方案

#1


63  

just a FYI

只是通知你

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

would actually be better like

那样会更好吗

string clean = Regex.Replace(q, @"[^\w\s]", string.Empty);

#2


16  

This:

这样的:

string clean = Regex.Replace(dirty, "[^a-zA-Z0-9\x20]", String.Empty);

\x20 is ascii hex for 'space' character

\x20是“空格”字符的ascii十六进制

you can add more individual characters that you want to be allowed. If you want for example "?" to be ok in the return string add \x3f.

您可以添加更多您希望允许的独立字符。如果你想要“?”在返回字符串中是ok的,添加\x3f。

#3


12  

I got it:

我明白了:

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

Didn't know you could put \s in the brackets

我不知道你可以把它放在括号里。

#4


3  

I suspect ^ doesn't work the way you think it does outside of a character class.

我怀疑^不工作你的思维方式以外的字符类。

What you're telling it to do is replace everything that isn't an alphanumeric with an empty string, OR any leading space. I think what you mean to say is that spaces are ok to not replace - try moving the \s into the [] class.

你要让它做的是用空字符串或任何前导空间替换所有非字母数字。我想你的意思是说,空格是可以不用替换的——试着把\s移到[]类中。

#5


2  

The following regex is for space inclusion in textbox.

下面的regex用于在文本框中包含空格。

Regex r = new Regex("^[a-zA-Z\\s]+");
r.IsMatch(textbox1.text);

This works fine for me.

这对我来说没问题。

#6


1  

There appear to be two problems.

这里似乎有两个问题。

  1. You're using the ^ outside a [] which matches the start of the line
  2. 你用外^[]匹配的开始
  3. You're not using a * or + which means you will only match a single character.
  4. 您没有使用*或+,这意味着您将只匹配一个字符。

I think you want the following regex @"([^a-zA-Z0-9\s])+"

我认为你想要下面的正则表达式@”([^ a-zA-Z0-9 \ s])+”

#7


0  

The circumflex inside the square brackets means all characters except the subsequent range. You want a circumflex outside of square brackets.

方括号内的圆圈表示除后续范围外的所有字符。你想要在方括号外有一个圆弧。

#1


63  

just a FYI

只是通知你

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

would actually be better like

那样会更好吗

string clean = Regex.Replace(q, @"[^\w\s]", string.Empty);

#2


16  

This:

这样的:

string clean = Regex.Replace(dirty, "[^a-zA-Z0-9\x20]", String.Empty);

\x20 is ascii hex for 'space' character

\x20是“空格”字符的ascii十六进制

you can add more individual characters that you want to be allowed. If you want for example "?" to be ok in the return string add \x3f.

您可以添加更多您希望允许的独立字符。如果你想要“?”在返回字符串中是ok的,添加\x3f。

#3


12  

I got it:

我明白了:

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

Didn't know you could put \s in the brackets

我不知道你可以把它放在括号里。

#4


3  

I suspect ^ doesn't work the way you think it does outside of a character class.

我怀疑^不工作你的思维方式以外的字符类。

What you're telling it to do is replace everything that isn't an alphanumeric with an empty string, OR any leading space. I think what you mean to say is that spaces are ok to not replace - try moving the \s into the [] class.

你要让它做的是用空字符串或任何前导空间替换所有非字母数字。我想你的意思是说,空格是可以不用替换的——试着把\s移到[]类中。

#5


2  

The following regex is for space inclusion in textbox.

下面的regex用于在文本框中包含空格。

Regex r = new Regex("^[a-zA-Z\\s]+");
r.IsMatch(textbox1.text);

This works fine for me.

这对我来说没问题。

#6


1  

There appear to be two problems.

这里似乎有两个问题。

  1. You're using the ^ outside a [] which matches the start of the line
  2. 你用外^[]匹配的开始
  3. You're not using a * or + which means you will only match a single character.
  4. 您没有使用*或+,这意味着您将只匹配一个字符。

I think you want the following regex @"([^a-zA-Z0-9\s])+"

我认为你想要下面的正则表达式@”([^ a-zA-Z0-9 \ s])+”

#7


0  

The circumflex inside the square brackets means all characters except the subsequent range. You want a circumflex outside of square brackets.

方括号内的圆圈表示除后续范围外的所有字符。你想要在方括号外有一个圆弧。