正则表达式:0 - 9 a - z - z或任何这些的! # $ % & * + - / = ? ^ _ { | } ~

时间:2022-10-29 21:15:10

I need to regex string myString to only have:

我需要regex字符串myString只有:

  • 0-9
  • 0 - 9
  • A-Z or a-z
  • a - z、a - z
  • any of these characters '!#$%&'*+-/=?^_`{|}~.
  • 这些角色的! # $ % & ' * +,/ = ? ^ _ { | } ~。

This is my code line:

这是我的代码行:

new Regex("[a-zA-Z0-9]").IsMatch(myString);

So far I have [a-zA-Z0-9] and this works fine for the first two listitems. Currently tearing my hair out (and it's so nice I want to keep it) over metacharacters and getting nowhere.

到目前为止,我有[a-zA-Z0-9],这对前两个listitem很管用。现在,把我的头发剪掉(而且我想保留它)超过了元字符,而且没有得到任何地方。

Any help would be greatly appreciated. Thanks. Dave

非常感谢您的帮助。谢谢。戴夫

4 个解决方案

#1


4  

Hi there If you want only the listed characters in your string it is very simple.but you need to match beginning an end of line

如果你想要字符串中列出的字符,那就很简单了。但是你需要匹配开始的底线。

new Regex("^[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~.-]*$").IsMatch(myString);

#2


3  

"[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~.-]"

check that ("-") minus sign be the last char in a [] sequence

检查(“-”)减号是[]序列中的最后一个字符。

#3


2  

Meta characters are fine between brackets, as long as you escape the significant ones. Moreover the dash MUST be the last one of your sequence.

元字符在括号之间是可以的,只要你避开了重要的字符。此外,破折号必须是你的最后一个序列。

new Regex("[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~-]").IsMatch(myString);

#4


1  

Try:

试一试:

var re = "[a-zA-Z0-9" +  Regex.Escape("'!#$%&'*+-/=?^_`{|}~.") + "]";

#1


4  

Hi there If you want only the listed characters in your string it is very simple.but you need to match beginning an end of line

如果你想要字符串中列出的字符,那就很简单了。但是你需要匹配开始的底线。

new Regex("^[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~.-]*$").IsMatch(myString);

#2


3  

"[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~.-]"

check that ("-") minus sign be the last char in a [] sequence

检查(“-”)减号是[]序列中的最后一个字符。

#3


2  

Meta characters are fine between brackets, as long as you escape the significant ones. Moreover the dash MUST be the last one of your sequence.

元字符在括号之间是可以的,只要你避开了重要的字符。此外,破折号必须是你的最后一个序列。

new Regex("[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~-]").IsMatch(myString);

#4


1  

Try:

试一试:

var re = "[a-zA-Z0-9" +  Regex.Escape("'!#$%&'*+-/=?^_`{|}~.") + "]";