使用Java模式进行特殊字符检查

使用Java模式进行特殊字符检查,java,regex,pattern-matching,special-characters,matcher,Java,Regex,Pattern Matching,Special Characters,Matcher,我想用下面的信息生成两个模式 1) 无法在帐户表单的“姓名”、“电子邮件”和“电话号码”字段中输入以下特殊字符: 模式“[]:;|=+*?/\, 名称不能以句点开头 2) 无法在“公司地址”字段中输入以下特殊字符: 模式\| 请给我一个主意 提前感谢您可以使用String.contains()方法,而不是使用正则表达式(您显然对此不放心) 但是,如果您必须使用正则表达式,如Mayur Patel所说,“[ab]”基本上是指a或b!您应该查看regularexpressions.info尝试这些模

我想用下面的信息生成两个模式

1) 无法在帐户表单的“姓名”、“电子邮件”和“电话号码”字段中输入以下特殊字符:

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

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

模式<>\|

请给我一个主意


提前感谢

您可以使用
String.contains()
方法,而不是使用正则表达式(您显然对此不放心)


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

尝试这些模式

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)
第一点

(?i)^([a-z][^”\[:\]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\=\++]$
对于2st点

(?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)
1st模式
“(?i)”+--将正则表达式的其余部分与选项匹配:不区分大小写(i)
“^”+--在行首(字符串开头或换行符后)断言位置
(“+--匹配下面的正则表达式,并将其匹配项捕获到反向引用1中
“[a-z]”+--在“a”和“z”之间匹配单个字符”
“[^\”\\[:\\]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\+\\\\\\\?\\\\/\r\n]”+--匹配下表中不存在的单个字符
--角色“”
--[字]
--字符“:”
--A]性格
--性格
--字符“=”
--A+字符
--A*字符
--角色
--其中一个字符“”
--一个\字符
--A/字符
--回车符
--换行字符
“+”+--在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
")" +
“$”--在行尾(字符串末尾或换行符之前)断言位置
第二种模式
“(?i)”+--将正则表达式的其余部分与选项匹配:不区分大小写(i)
“^”+--在行首(字符串开头或换行符后)断言位置
(“+--匹配下面的正则表达式,并将其匹配项捕获到反向引用1中
“[a-z]”+--在“a”和“z”之间匹配单个字符”
“[^\\\/\\\\r\n]”+--匹配下表中不存在的单个字符
--其中一个字符“”
--一个\字符
--A/字符
--性格
--回车符
--换行字符
“+”+--在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
")" +
“$”--在行尾(字符串末尾或换行符之前)断言位置
代码

try {
    boolean foundMatch = subjectString.matches("(?i)^([a-z][^\"\\[:\\]|=+*?<>\\\\/\\r\\n]+)$");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}
试试看{
boolean foundMatch=subjectString.matches((?i)^([a-z][^\”\\[:\\]\;=+*?\\\/\\r\\n]+)$);
}捕获(模式语法异常){
//正则表达式中的语法错误
}

下面是我问题的解决方案

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

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

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

非常感谢塞利安和安迪的帮助,这真的帮了我很大的忙


再次感谢:)

?对于第一个模式[[]:;/\“\124;=+*?,]对于第二个模式[/\”\124;]这是正确的吗?我不知道如何检查名称不能以句号开头,有人能建议吗?String.contains这是检查特殊字符的标准方法,Regex模式是我们可以选择的选项,但我不太知道如何创建我指定的模式dexcellent Cylian,非常感谢您的详细解释,我我只是想确认,对于第一点,这也是检查名称不能以句点开头吗?我只是想确认,对于第一点,这也是检查名称不能以句点开头吗?对于第二点,它给出了编译错误,例如无效的转义序列(有效的是\b\t\n\f\r\“\”)您能验证一下吗?@MayurPatel我可以告诉您,第一个正则表达式并不关心字符串是否以period@MayurPatel:我在这里提供的正则表达式是基本的,需要根据编译器的需要进行转义。@安迪:该模式与任何以
点开始的字符串都不匹配。
try {
    boolean foundMatch = subjectString.matches("(?i)^([a-z][^\"\\[:\\]|=+*?<>\\\\/\\r\\n]+)$");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}