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

/**
 * 
 */

/**
 * @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();
		}

	}

}





工作中遇到一个和技术有关的导致用户不能正常使用软件的问题,只是一个正则表达式排除掉前面 的UL就可以,技术可能也没太重视,那么用户的问题就转到我这里来了,还不能解决,很是不爽。问题的解决思路大体就是这样,再将这个字符串和其它的拼接在一起就和原来 的没什么区别


//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));
  


}



0797-2109|010791119697|郭华英|13879757233
0797-2201|UL801010294|卓喜琳|15570071192
0797-2299|UL801001967|邹明华|13763980460
0797-2299|UL801001971|郭少丽|15807977751
0797-2339|010791120440|胡声强|13479450289


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值