随便写下 String type = "BTSAE[3][REAL_REAK_BTSAE_404]"; Pattern p = Pattern.compile("\\[[1-9]+(\\d)*\\]"); Matcher m = p.matcher(type); if (m.find()) { String ret = m.group(); if (!ret.equals("")) System.out.println(ret.substring(1, ret.length() - 1)); }
补充:想要0就这样 Pattern p = Pattern.compile("\\[[[1-9]+(\\d)*|0]\\]");
String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
String reg = "^.*?\\[([^\\[\\]]*)\\].*?$";
System.out.println(str.replaceAll(reg, "$1"));
/**
* create date:2009-6-12 author:Administrator
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String type = "BTSAE[3][REAL_REAK_BTSAE_404]";
Pattern p = Pattern.compile("([\\d]{1,3})"); // System.out.println("the type is.." + type);
Matcher m = p.matcher(type);
if (m.find()) {
System.out.println(m.group()); }
}
String type = "BTSAE[3][REAL_REAK_BTSAE_404]";
Pattern p = Pattern.compile("\\[[1-9]+(\\d)*\\]");
Matcher m = p.matcher(type);
if (m.find()) {
String ret = m.group();
if (!ret.equals(""))
System.out.println(ret.substring(1, ret.length() - 1));
}
Pattern p = Pattern.compile("\\[[[1-9]+(\\d)*|0]\\]");
String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
String reg="^.*?\\[(\\d+)\\].*?$";
System.out.println(str.replaceAll(reg, "$1"));
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);
}
}
Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
if(m.find()){
System.out.println(m.group());
}
楼上的这种写法好像对[]里面的数字个数有限制,{1,3}只能取到前3位,希望是只要第一个[]是数字就提取出来,不管数字的有多少位
试试 BTSAE[3+3][REAL_REAK_BTSAE_404]这个应该对Pattern pattern = Pattern.compile("(?<=\\[)\\d+(?=\\])");
Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
if(m.find()){
System.out.println(m.group());
}
如果字符串为BTSAE[][343][REAL_REAK_BTSAE_404]就把第二个[]的值取出来。兄弟再帮忙想想
Pattern pattern = Pattern.compile("(?<=\\[)\\d+(?=\\])");
Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
if(m.find()){
System.out.println(m.group());
}
对 BTSAE[][343][REAL_REAK_BTSAE_404]
我这里的结果是 343(?<=\\[)\\d+(?=\\])(?<=\\[) 断言 \\d+ 前面的是 [
\\d+ 匹配至少一个数字
(?=\\]) 断言 \\d+ 后面的是 ]理论和实际上都不会匹配 []
BTSAE[][343][REAL_REAK_BTSAE_404] 期望 m.find() 执行结果为false就可以了BTSAE[1+3][343][REAL_REAK_BTSAE_404] 期望 m.find() 执行结果为false就可以了BTSAE[4567][343][REAL_REAK_BTSAE_404] 期望 m.find() 执行结果为true
麻烦楼上的高人帮忙解答,感谢
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));
}
}
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
}
}