我想用以下信息生成两种模式,
1) 账户表单中的名字、姓氏、电子邮件、电话号码字段中不能输入以下特殊字符:
模式 " [ ] : ; | = + * ? < > / \ , 名称不能以句点开头
2)以下特殊字符不能在公司地址字段中输入:
模式< > / \ |
请给我一个想法。
提前致谢
我想用以下信息生成两种模式,
1) 账户表单中的名字、姓氏、电子邮件、电话号码字段中不能输入以下特殊字符:
模式 " [ ] : ; | = + * ? < > / \ , 名称不能以句点开头
2)以下特殊字符不能在公司地址字段中输入:
模式< > / \ |
请给我一个想法。
提前致谢
试试这些模式
第 1 点
(?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
}
String.contains()
您可以使用该方法,而不是使用您显然没有信心的正则表达式。
但是,如果您必须使用正则表达式,就像 Mayur Patel 所说的那样,“ [ab]
”基本上意味着 a 或 b !您应该查看正则表达式.info
以下是我的问题的解决方案
1) (?i)^([az][^\"\[:\]|=+*.?<>\\/\r\n]+)$
2) (?i)^([az][^\"<>|\\/\r\n]+)$
我还在 1) 点中添加了句点符号,用于检查不以句点符号开头的名称。
非常感谢 Cylian 和 Andy 的帮助,它真的帮了我很多。
再次感谢 :)