专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 匹配正则表达式字母和数字

匹配正则表达式字母和数字

更新时间:2021-10-28 10:57:04 来源:赢咖4 浏览795次

如何匹配正则表达式字母和数字?小编来给大家举例说明:

/**
 * 
 */ 
/**
 * @author dell
 *
 */
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern; 
public class HelloWorld { 
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stu
		// String s = "is is of of";
		// String s="12 aa bb 好2";
		// String regex="(.)\\1";
		// String s="abcs123abc123abc123";
		String s = "UL8010abcd";
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String time = df.format(date);
		String savedtxt = "D:\\testOK_" + time + ".txt";
		// String regex ="(\\d+)(\\w+)\\1";
		String regex = "(^UL)(\\d+)";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(s); 
		FileWriter fileWriter = null; 
		if (m.find()) {
			s = m.group(0).substring(2);
			// s = s.substring(2);
			// s=Integer.parseInt(s)+"|"+123+"|"
			s += "|" + 123 + "|";
		} else {
			s += "|" + 123 + "|";
		}
		try {
			fileWriter = new FileWriter(savedtxt, true);  // 加 true 等于附加,
			// fileWriter.write(s);
			fileWriter.write(s + "\r\n"); 
			// System.out.println(Integer.parseInt(s)+"|"+123+"|");
			System.out.println("OK");
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			fileWriter.flush();
			fileWriter.close();
		} 
	} 
}

问题的解决思路大体就是这样,再将这个字符串和其它的拼接在一起就和原来的没什么区别

//String s="abcs123abc123abc123";
String s="UL8010";
//String regex ="(\\d+)(\\w+)\\1";
String regex ="(^UL)(\\d+)";
   Pattern p=Pattern.compile(regex);
 Matcher m=p.matcher(s);
 while(m.find()){
 s=s.substring(2);
  System.out.println(Integer.parseInt(s));
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>