检查字符串是否包含某些charecters Java正则表达式
在以下条件下,我要检查字符串的true或false:检查字符串是否包含某些charecters Java正则表达式,java,regex,Java,Regex,在以下条件下,我要检查字符串的true或false: 1. the string must be 5 charecters long 2. 3rd charecter should be 'b' 3. the string must contain special charecter like '?' or '@' and '$' 我不知道该做什么,但我还是做了: System.out.println("//b//b//b?//b//b//b" ,"akb?g");//this is tota
1. the string must be 5 charecters long
2. 3rd charecter should be 'b'
3. the string must contain special charecter like '?' or '@' and '$'
我不知道该做什么,但我还是做了:
System.out.println("//b//b//b?//b//b//b" ,"akb?g");//this is totally wrong though
尝试使用以下模式:
^(?=.*[?@$]).{2}b.{2}$
if ("invalid".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("invalid matches");
}
if ("12b?3".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("12b?3 matches");
}
下面是一段代码片段,显示了如何使用此模式:
^(?=.*[?@$]).{2}b.{2}$
if ("invalid".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("invalid matches");
}
if ("12b?3".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("12b?3 matches");
}
以下是该模式的简要说明:
^ from the start of the string
(?=.*[?@$]) assert that we see a special character anywhere, at least once
.{2} then match any two characters
b match a 'b' for the 3rd character
.{2} then match any two characters again
$ end of string
尝试使用以下模式:
^(?=.*[?@$]).{2}b.{2}$
if ("invalid".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("invalid matches");
}
if ("12b?3".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("12b?3 matches");
}
下面是一段代码片段,显示了如何使用此模式:
^(?=.*[?@$]).{2}b.{2}$
if ("invalid".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("invalid matches");
}
if ("12b?3".matches("(?=.*[?@$]).{2}b.{2}")) {
System.out.println("12b?3 matches");
}
以下是该模式的简要说明:
^ from the start of the string
(?=.*[?@$]) assert that we see a special character anywhere, at least once
.{2} then match any two characters
b match a 'b' for the 3rd character
.{2} then match any two characters again
$ end of string
如果您的意思是“?”或“@”和“$”请使用此选项
(
(?=.*[\$?]) // Must contain either $ or ?
(?=.*[@?]) // Must contain either @ or ?
..b..$ // Third character should be b
)
一行
((?=.*[\$?])(?=.*[@?])..b..$)
如果您的意思是“?”或“@”和“$”请使用此选项
(
(?=.*[\$?]) // Must contain either $ or ?
(?=.*[@?]) // Must contain either @ or ?
..b..$ // Third character should be b
)
一行
((?=.*[\$?])(?=.*[@?])..b..$)
你的//b应该是什么意思?你的一个字符串没有闭合。你的//b应该是什么意思?你的一个字符串没有闭合。请你解释一下,这会很有帮助。感谢你的努力。谢谢,请你解释一下,这会很有帮助的。感谢你的努力。非常感谢。