使用Java Pattern进行特殊字符检查

时间:2012-05-26 11:32:51

标签: java regex pattern-matching special-characters matcher

我想用以下信息生成两种模式,

1)以下特殊字符无法输入帐户表格中的名字,姓氏,电子邮件,电话号码字段:

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

2)无法在公司地址栏中输入以下特殊字符:

模式< > / \ |

请给我一个主意。

提前致谢

3 个答案:

答案 0 :(得分:4)

尝试这些模式

第一点

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

第2点

(?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 :(得分:1)

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

但是,如果你必须使用正则表达式,就像Mayur Patel所说,“[ab]”基本上意味着a或b!你应该看看regularexpressions.info

答案 2 :(得分:1)

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

1)(?i)^([a-z] [^ \“\ [:\] | = + *。?&lt;&gt; \\ / \ r \ n] +)$

2)(?i)^([a-z] [^ \“&lt;&gt; | \\ / \ r \ n] +)$

我还在1)点中添加了句点符号,用于检查不带有句点符号的名称。

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

再次感谢:)

相关问题