正则表达式 匹配字母和数字
/** * */ /** * @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 + " "); // 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
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: js图片转base64编码压缩上传
- 下一篇: 字母,数字,下划线或者数字的正则表达式