您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Java中,要使用正则表达式匹配非数字字符,可以使用\D
或者[^0-9]
。下面是两个示例:
示例1:使用\D
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = "Hello123";
String regex = "\\D";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("找到非数字字符: " + matcher.group());
}
}
}
示例2:使用[^0-9]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = "Hello123";
String regex = "[^0-9]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("找到非数字字符: " + matcher.group());
}
}
}
这两个示例都会输出:
找到非数字字符: H
找到非数字字符: e
找到非数字字符: l
找到非数字字符: l
找到非数字字符: o
这表示它们成功地找到了字符串中的非数字字符。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。