使用Java模式检查特殊字符

问题描述 投票:0回答:3

我想生成带有以下信息的两种模式,

1)帐户表单中的名字、姓氏、电子邮件、电话号码字段中不能输入以下特殊字符:

模式 " [ ] : ; | = + * ? < > / \ , 名称不能以句点开头

2)公司地址字段中不能输入以下特殊字符:

图案 < > / \ |

请给我一个想法。

提前致谢

java regex special-characters
3个回答
4
投票

尝试这些模式

第一点

(?i)^([a-z][^"\[:\]\|=\+\*\?<>\\\/\r\n]+)$

第二点

(?i)^([a-z][^<>\\\/\|\r\n]+)$

说明

1st Pattern

"(?i)" +                               -- Match the remainder of the regex with the options: case insensitive (i)
"^" +                                  -- Assert position at the beginning of a line (at beginning of the string or after a line break character)
"(" +                                  -- Match the regular expression below and capture its match into backreference number 1
   "[a-z]" +                              -- Match a single character in the range between “a” and “z”
   "[^\"\\[:\\]\\|=\\+\\*\\?<>\\\\\\/\r\n]" +       -- Match a single character NOT present in the list below
                                             -- The character “"”
                                             -- A [ character
                                             -- The character “:”
                                             -- A ] character
                                             -- A | character
                                             -- The character “=”
                                             -- A + character
                                             -- A * character
                                             -- A ? character
                                             -- One of the characters “<>”
                                             -- A \ character
                                             -- A / character
                                             -- A carriage return character
                                             -- A line feed character
      "+" +                                  -- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"$"                                    -- Assert position at the end of a line (at the end of the string or before a line break character)


2nd Pattern

"(?i)" +                  -- Match the remainder of the regex with the options: case insensitive (i)
"^" +                     -- Assert position at the beginning of a line (at beginning of the string or after a line break character)
"(" +                     -- Match the regular expression below and capture its match into backreference number 1
   "[a-z]" +                 -- Match a single character in the range between “a” and “z”
   "[^<>\\\\\\/\\|\r\n]" +       -- Match a single character NOT present in the list below
                                -- One of the characters “<>”
                                -- A \ character
                                -- A / character
                                -- A | character
                                -- A carriage return character
                                -- A line feed character
      "+" +                     -- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"$"                       -- Assert position at the end of a line (at the end of the string or before a line break character)

代码

try {
    boolean foundMatch = subjectString.matches("(?i)^([a-z][^\"\\[:\\]|=+*?<>\\\\/\\r\\n]+)$");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

1
投票

您可以使用

String.contains()
方法,而不是使用您显然不自信的正则表达式。

但是,如果你必须使用正则表达式,就像 Mayur Patel 所说,“

[ab]
”基本上意味着 a 或 b !你应该查看正则表达式.info


1
投票

以下是我的问题的解决方案

1) (?i)^([a-z][^\"\[:\]|=+*.?<>\/ ]+)$

2) (?i)^([a-z][^\"<>|\/ ]+)$

我还在 1) 点中添加了句点符号,用于检查名称是否以句点符号开头。

非常感谢Cylian和Andy的帮助,这真的对我帮助很大。

再次感谢:)

© www.soinside.com 2019 - 2024. All rights reserved.