使用正则表达式选择除字母或数字外的所有字符

jst*_*rdo 2 regex

我有这个正则表达式:

^[a-zA-Z0-9]
Run Code Online (Sandbox Code Playgroud)

我试图选择除数字或字母之外的任何字符,但是在测试时,仅第一个字符被匹配。

当我使用

[a-zA-Z0-9]
Run Code Online (Sandbox Code Playgroud)

匹配项正确是数字和字母。我需要取消它,但^不能正常工作。

小智 6

以下是正则表达式的快速摘要,以及如何使用以下命令将查询集分组在一起。在您的情况下,将其放置在^内部[a-zA-Z0-9]即可达到所需的效果。

  .   Single character match except newline
  "." Anything in quotations marks literally
  A*  Zero or more occurrences of A
  A+  One or more occurrences of A
  A?  Zero or one occurrence of A
  A/B Match A only if followed by B
  ()  series of regexps grouped together
  []  Match any character in brackets
  [^] Do not match any character in brackets
  [-] Define range of characters to match
  ^   Match beginning of new line
  $   Match end of a line
  {}  Cardinality of pattern match set
  \   Escapes meta-characters
  |   Disjunctive matches, or operation match
Run Code Online (Sandbox Code Playgroud)


Sov*_*iut 5

将 放在^表达式的开头意味着“从字符串的开头搜索”。您需要将其放在方括号内以使其成为否定。

[^a-zA-Z0-9]
Run Code Online (Sandbox Code Playgroud)