gpt4 book ai didi

java - 缺少右括号

转载 作者:行者123 更新时间:2023-12-04 10:12:11 27 4
gpt4 key购买 nike

索引 13 附近的字符类中缺少右括号
|\?*<":>+[]/'
我的代码:

Pattern.compile("|\\?*<\":>+[]/'").matcher(name).matches()

最佳答案

您可以使用

Pattern.compile("[|\\\\?*<\":>+\\[\\]/']+").matcher(name).matches()

正则表达式的意思是:
  • [ - 一个积极的性格类的开始:
  • | - 一根管子
  • \\ - 反斜杠(需要在字符串文字中添加额外的反斜杠, "\\\\" )
  • ? - 问号
  • * - 星号
  • < - 开放式尖括号
  • " - 双引号
  • : - 冒号
  • > - 一个封闭的尖括号
  • + - 加分
  • \[ - 一个 [字符(当 [ 在字符类中时必须转义)
  • \] - 一个 ]字符(当 ] 在字符类中时必须转义)
  • / - 正斜杠
  • ' - 单引号
  • ]+ - 字符类结束,出现 1 次或多次。

  • 因此,这将验证仅包含这些字符的 1 次或多次出现的字符串。如果您需要相反的,请添加 ^第一次后 [ :
    Pattern.compile("[^|\\\\?*<\":>+\\[\\]/']+").matcher(name).matches()
    // ^

    Java demo :
    String name = "Wiktor Stribiżew";
    System.out.println(Pattern.compile("[^|\\\\?*<\":>+\\[\\]/']+").matcher(name).matches());
    // => true

    关于java - 缺少右括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61274273/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com