I use regex
我用正则表达式
r="[^A-Za-z0-9]+";
to detect if a string has one or more chars other than letters and digits;
检测字符串是否有一个或多个字母和数字以外的字符;
Then I tried the following:
然后我尝试了以下内容:
Pattern.compile(r).matcher(p).find();
I tested:
! @ # $ % ^ & * ( ) + =- [ ] \ ' ; , . / { } | " : < > ? ~ _ `
Most of the time, it works except backsplash \ and caret ^.
大多数情况下,它的工作原理除了backsplash \和caret ^。
e.g.
String p = "abcAsd10^" (return false)
String p = "abcAsd10\\" (return false)
Anything I miss?
我想念的一切?
7 个解决方案
#1
2
The following code prints "Found: true" when I compile and run it:
当我编译并运行它时,以下代码打印“Found:true”:
class T
{
public static void main (String [] args)
{
String thePattern = "[^A-Za-z0-9]+";
String theInput = "abskKel35^";
boolean isFound = Pattern.compile(thePattern).matcher(theInput).find();
System.out.println("Found: " + isFound);
}
}
Not sure why you would see a different result...
不确定为什么你会看到不同的结果......
#2
1
Shouldn't that be:
不应该是:
r="[^A-Za-z0-9]+";
In your question you write a_z (underscore)
在你的问题中你写a_z(下划线)
#3
1
When I run this code on my machine it prints true..
当我在我的机器上运行此代码时,它打印为真..
String r="[^A-Za-z0-9]+";
String p = "abcAsd10\\" ;
Matcher m = Pattern.compile(r).matcher(p);
System.out.println(m.find());
true
#4
1
You could also change only:
你也可以只改变:
[\w&&^_]+
Where:
-
\w
A word character with underscore:[a-zA-Z_0-9]
-
\W
A non-word character:[^\w]
\ w带下划线的单词字符:[a-zA-Z_0-9]
\ W一个非单词字符:[^ \ w]
See more in class Pattern
在课程模式中查看更多内容
#5
0
Try quoting your input into the matcher.
尝试将您的输入引用到匹配器中。
Pattern.quote(p)
#6
0
Simply do:
p.matches(".*\\W.*"); //return true if contains one or more special character
#7
0
Sorry, I had
对不起,我有
r="[^A-za-z0-9]+"; (with little "z", i.e. A-z).
That causes returning "false". But why it works with other special chars except backsplash \ and caret ^?
这导致返回“假”。但是为什么它与其他特殊字符一起工作,除了backsplash \和caret ^?
#1
2
The following code prints "Found: true" when I compile and run it:
当我编译并运行它时,以下代码打印“Found:true”:
class T
{
public static void main (String [] args)
{
String thePattern = "[^A-Za-z0-9]+";
String theInput = "abskKel35^";
boolean isFound = Pattern.compile(thePattern).matcher(theInput).find();
System.out.println("Found: " + isFound);
}
}
Not sure why you would see a different result...
不确定为什么你会看到不同的结果......
#2
1
Shouldn't that be:
不应该是:
r="[^A-Za-z0-9]+";
In your question you write a_z (underscore)
在你的问题中你写a_z(下划线)
#3
1
When I run this code on my machine it prints true..
当我在我的机器上运行此代码时,它打印为真..
String r="[^A-Za-z0-9]+";
String p = "abcAsd10\\" ;
Matcher m = Pattern.compile(r).matcher(p);
System.out.println(m.find());
true
#4
1
You could also change only:
你也可以只改变:
[\w&&^_]+
Where:
-
\w
A word character with underscore:[a-zA-Z_0-9]
-
\W
A non-word character:[^\w]
\ w带下划线的单词字符:[a-zA-Z_0-9]
\ W一个非单词字符:[^ \ w]
See more in class Pattern
在课程模式中查看更多内容
#5
0
Try quoting your input into the matcher.
尝试将您的输入引用到匹配器中。
Pattern.quote(p)
#6
0
Simply do:
p.matches(".*\\W.*"); //return true if contains one or more special character
#7
0
Sorry, I had
对不起,我有
r="[^A-za-z0-9]+"; (with little "z", i.e. A-z).
That causes returning "false". But why it works with other special chars except backsplash \ and caret ^?
这导致返回“假”。但是为什么它与其他特殊字符一起工作,除了backsplash \和caret ^?