更新時(shí)間:2021-10-28 10:57:04 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1253次
如何匹配正則表達(dá)式字母和數(shù)字?小編來(lái)給大家舉例說(shuō)明:
/**
*
*/
/**
* @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();
}
}
}
問(wèn)題的解決思路大體就是這樣,再將這個(gè)字符串和其它的拼接在一起就和原來(lái)的沒(méi)什么區(qū)別
//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基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)