Java Regex格式

时间:2018-08-14 04:00:07

标签: java regex

我正在Java程序中使用以下正则表达式。

sayHi()function

我从任何站点获得的此正则表达式。只是想知道此正则表达式中将要遵循的内容

Person.prototype.sayHi

以上部分将做什么。上面提到的字符是什么符号?

此外,通过使用上述正则表达式,我能够在Java程序中匹配单个斜杠“ \”,但不能匹配双反斜杠“ \”。怎么做?

此正则表达式有问题吗?我想使用Java在我的代码中排除所有这些字符。此正则表达式可以在javascript代码上正常工作,但不能在Java代码上正常工作。

1 个答案:

答案 0 :(得分:0)

我相信正则表达式并不是您在问题中所写的。您写的是Java字符串文字:

String regex = "^[-!$%^&*()+|~=`\\\\#{}\\[\\]:;<>?,.\\/@\\b\\r\\t\\Z\\%\\n\\0\\_\\'\\\"\\\\]$";

如果确实如此,则它是无效的正则表达式,使用时会引起PatternSyntaxException
如果不是这种情况,则正则表达式完全没有意义,但请参阅此答案的后面。
首先,我假设它字符串文字。

Java字符串文字使用\来转义特殊字符,如Java Language Specification中所述,因此上面的代码是此正则表达式:

^[-!$%^&*()+|~=`\\#{}\[\]:;<>?,.\/@\b\r\t\Z\%\n\0\_\'\"\\]$

正则表达式也使用\来转义特殊字符,如Pattern的javadoc中所述,它表示:

^                  Match beginning of input
[                  Start of character class, i.e. match any of the following characters:
  -                  Minus sign. Is special in a character class,
                         but can be unescaped if first or last
  !$%^&*()+|~=`      Any of those characters
  \\                 \ is the escape character, so it is escaped
  #{}                Any of those characters
  \[\]               [] brackets have special meaning, so they are escaped
  :;<>?,.            Any of those characters
  \/                 Unnecessary escape of /, probably because regex was designed
                         for non-Java use
  @                  That character
  \b                 Undefined, causes PatternSyntaxException
  \r                 Carriage-return character ('\u000D')
  \t                 Tab character ('\u0009')
  \Z                 Undefined, causes PatternSyntaxException
  \%                 Unnecessary escape of %
  \n                 Newline (line feed) character ('\u000A')
  \0                 Undefined, causes PatternSyntaxException
  \_                 Unnecessary escape of _
  \'                 Unnecessary escape of '
  \"                 Unnecessary escape of "
  \\                 Redundant, since \ is already added earlier
]                  End of character class
$                  Match end of input

如果正则表达式确实是您编写的:

^                  Match beginning of input
[                  Start of character class, i.e. match any of the following characters:
  -                  Minus sign. Is special in a character class,
                         but can be unescaped if first or last
  !$%^&*()+|~=`      Any of those characters
  \\                 \ is the escape character, so it is escaped
  \\                 Redundant, since \ is already added earlier
  #{}                Any of those characters
  \\                 Redundant
  [                  Start of nested character class
    \\                 Redundant
  ]                  End of nested character class
  :;<>?,.            Any of those characters
  \\                 Redundant
  /@                 Any of those characters
  \\                 Redundant
  b                  That character
  \\                 Redundant
  r                  That character
  \\                 Redundant
  t                  That character
  \\                 Redundant
  Z                  That character
  \\                 Redundant
  %                  That character
  \\                 Redundant
  n                  That character
  \\                 Redundant
  0                  That character
  \\                 Redundant
  _                  That character
  \\                 Redundant
  '                  That character
  \\                 Redundant
  \"                 Unnecessary escape of "
  \\                 Redundant
  \\                 Redundant
]                  End of character class
$                  Match end of input

删除所有多余和不必要的部分,它与:

^[-!$%^&*()+|~=`\\#{}:;<>?,./@brtZ%n0_'"]$

其中,作为Java字符串文字是:

String regex = "^[-!$%^&*()+|~=`\\\\#{}:;<>?,./@brtZ%n0_'\"]$";