介绍
怎么在Java中对字符串类进行正则操作?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<强>一应用正则表达式强>
1代码
public class RegExp { public 才能;static  void main (String [], args), throws 例外 {才能 ,,,if (“123“.matches (“\ \ d +“)) ,,,{ ,,,,,System.out.println(“由数字组成!“); ,,,}, ,,,{ ,,,,,System.out.println(“不”是由数字组成!“); ,,,} ,,} }
2运行
由数字组成。
引用><强>二字符串替换——过滤字符串中的数字强>
1代码
public class 子字符串 { public 才能;static  void main (String [], args), throws 例外 {才能 ,,,String str =,“a1b22c333d4444e55555f6666666g"; ,,,String regex =,“[0 - 9] +“,//,数字出现1次或多次 ,,,//String regex =,“\ \ d +“,//,数字出现1次或多次 ,,,System.out.println (str.replaceAll(正则表达式,,““)); ,,} }2运行
英语字母
引用><强>三正则验证邮箱格式强>
1代码
import java.util。*; public class  EmailValidation { public 才能;static  void main (String [], args), throws 例外 {才能 ,,,String str =,空; ,,,String regex =,“\ \ w + @ \ \ w +。\ \ w +“;; ,,,Scanner reader =, new 扫描仪(系统); ,才能做 ,,,{ ,,,,,System.out.print(“请输入一个有效的邮件地址:“); ,,,,,str =, reader.next (); ,,,,,System.out.println (str); ,,,},while (! str.matches(正则表达式)); ,,,System.out.println(“邮件地址有效!谢谢注册!“); ,,,reader.close (); ,,} }2运行
请输入一个有效的邮件地址:4月好
引用>
4月好
请输入一个有效的邮件地址:yy@qq.com
yy@qq.com
邮件地址有效!谢谢注册!怎么在Java中对字符串类进行正则操作