English 中文(简体)
使用 Java 模式的特殊字符检查
原标题:Special Character check using Java Pattern

我想产生两种模式 以下面的信息,

(1) 无法在账户窗体中以名、姓、电子邮件、电话号码字段输入下列特殊字符:

Pattern " [ ] : ; | = + * ? < > / , Names cannot start with a period

(2) 无法在公司地址字段中输入下列特殊字符:

< parcter & lt; & gt; /

请给我个想法

提前感谢

最佳回答

< 强 > 尝试这些图案 < / 强 >

用于第一个点

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

用于第二点

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

<强 > Explants

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”
   "[^"\[:\]\|=\+\*\?<>\\\/
]" +       -- 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”
   "[^<>\\\/\|
]" +       -- 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
}
问题回答

您可以使用 < code> string. contains () 方法,而不是使用您显然不信任的正则表达式。

但是,如果你必须使用正gex,就像Mayur Patel所说的,“[ab] ”基本上是指 a 或 b! 您应该检查正则表达式。 info

下面是解决我问题的办法

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

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

我还在1处添加了周期符号,用于检查不以周期符号启动名称。

非常感谢赛连和安迪的帮助 这真的帮了我很多

再次感谢:)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签