更新時間:2021-10-28 10:57:04 來源:動力節點 瀏覽1114次
如何匹配正則表達式字母和數字?小編來給大家舉例說明:
/**
*
*/
/**
* @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));
}
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習