62,615
社区成员
发帖
与我相关
我的任务
分享
Pattern pattern = Pattern.compile("(?<=\\[)[^\\[\\]]*(?=\\])");
Matcher m = pattern.matcher("BTSAE[-343][REAL_REAK_BTSAE_404]");
if (m.find()) {// if 取第一个,while 取全部
if(Pattern.matches("^-?\\d+$", m.group())){
System.out.println(m.group());// 如果是整数则打印
}else{
System.out.println(false);// 不是整数,打印 false
}
}
import java.util.regex.*;
public class RegTest{
public static void main(String[] args){
String s="BTSAE[4567][343][REAL_REAK_BTSAE_404]";
Pattern p=Pattern.compile("BTSAE\\[(\\d+)\\].");
Matcher m=p.matcher(s);
System.out.println(m.find());
System.out.println(m.group(1));
}
}
Pattern pattern = Pattern.compile("(?<=\\[)\\d+(?=\\])");
Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
if(m.find()){
System.out.println(m.group());
}
package com.ricky.www;
/*
* BTSAE[3][REAL_REAK_BTSAE_404] ,要写个正则表达式提取第一个[]之间的数字出来,急啊
*/
public class Test{
public static void main(String[] args){
String message = "BTSAE[313543][REAL_REAK_BTSAE_404]";
String regex = "[^\\[]*\\[(\\d+).*";
String result = message.replaceAll(regex,"$1");
System.out.println(result);
}
}
String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
String reg="^.*?\\[(\\d+)\\].*?$";
System.out.println(str.replaceAll(reg, "$1"));
String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
String reg = "^.*?\\[([^\\[\\]]*)\\].*?$";
System.out.println(str.replaceAll(reg, "$1"));