求助这个正则表达式如何写

fuyingch 2009-06-12 04:16:57
BTSAE[3][REAL_REAK_BTSAE_404] ,要写个正则表达式提取第一个[]之间的数字出来,急啊
...全文
65 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
sd5816690 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 fuyingch 的回复:]
我的意思好像有点没有表达清楚,不应该用匹配第一个[]来描述, 这个正则表达式的功能只用于提取第一个[]里面的数字,请注意是第一个[],而不是第一个出现数字的[],当第一个[]不是数字是,不满足条件就可以了,不需要再去寻找下一个满足条件的[]。举例
BTSAE[][343][REAL_REAK_BTSAE_404] 期望 m.find() 执行结果为false就可以了

BTSAE[1+3][343][REAL_REAK_BTSAE_404] 期望 m.find() 执行结果为false就可以了

BTSAE…
[/Quote]


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
}
}
bigbug9002 2009-06-18
  • 打赏
  • 举报
回复
正在学习正则中,不知道下面的代码能不能满足楼主的要求:

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));
}
}
fuyingch 2009-06-18
  • 打赏
  • 举报
回复
再补充一下BTSAE[4567][343][REAL_REAK_BTSAE_404] 期望 m.find() 执行结果为true,并能m.group()直接返回4567
麻烦楼上的高人帮忙解答,感谢
fuyingch 2009-06-18
  • 打赏
  • 举报
回复
我的意思好像有点没有表达清楚,不应该用匹配第一个[]来描述, 这个正则表达式的功能只用于提取第一个[]里面的数字,请注意是第一个[],而不是第一个出现数字的[],当第一个[]不是数字是,不满足条件就可以了,不需要再去寻找下一个满足条件的[]。举例
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
sd5816690 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 fuyingch 的回复:]
谢谢楼上的兄弟,不过还有点缺陷。如果BTSAE[][343][REAL_REAK_BTSAE_404],就不对了,一定是匹配第一个[]的,
如果字符串为BTSAE[][343][REAL_REAK_BTSAE_404]就把第二个[]的值取出来。兄弟再帮忙想想
[/Quote]
不会吧
对 BTSAE[][343][REAL_REAK_BTSAE_404]
我这里的结果是 343

(?<=\\[)\\d+(?=\\])

(?<=\\[) 断言 \\d+ 前面的是 [
\\d+ 匹配至少一个数字
(?=\\]) 断言 \\d+ 后面的是 ]

理论和实际上都不会匹配 []
fuyingch 2009-06-18
  • 打赏
  • 举报
回复
谢谢楼上的兄弟,不过还有点缺陷。如果BTSAE[][343][REAL_REAK_BTSAE_404],就不对了,一定是匹配第一个[]的,
如果字符串为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());
}
sd5816690 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 fuyingch 的回复:]
希望是用正则表达式能够直接提取,不要用replaceAll方式完成。

Pattern pattern = Pattern.compile("([\\d+]{1,3})");
Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
if(m.find()){
System.out.println(m.group());
}
楼上的这种写法好像对[]里面的数字个数有限制,{1,3}只能取到前3位,希望是只要第一个[]是数字就提取出来,不管数字的有多少位
[/Quote]
LZ 不引用,还没发现这个是错的。。。
试试 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());
}
fuyingch 2009-06-18
  • 打赏
  • 举报
回复
希望是用正则表达式能够直接提取,不要用replaceAll方式完成。

Pattern pattern = Pattern.compile("([\\d+]{1,3})");
Matcher m = pattern.matcher("BTSAE[343][REAL_REAK_BTSAE_404]");
if(m.find()){
System.out.println(m.group());
}
楼上的这种写法好像对[]里面的数字个数有限制,{1,3}只能取到前3位,希望是只要第一个[]是数字就提取出来,不管数字的有多少位
tlowl 2009-06-18
  • 打赏
  • 举报
回复
我认为只用一个正则表达式的话,没有办法完全实现你的要求。
分开写的话,判断的正则:
^[^\\[|\\]]*\\[\\d+\\]
取数的正则:
\\[\\d+\\]
取数的正则结果还需要截取一下
micsolaris 2009-06-12
  • 打赏
  • 举报
回复
尽量做到少用.*?类似这样的写法,特别是对于匹配很长的字符串的时候,这样相对比较慢些。
micsolaris 2009-06-12
  • 打赏
  • 举报
回复

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);
}
}
jkleeo 2009-06-12
  • 打赏
  • 举报
回复
看不懂2楼写的什么意思.
nnb200890 2009-06-12
  • 打赏
  • 举报
回复
up!!
possibleonline 2009-06-12
  • 打赏
  • 举报
回复

String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
String reg="^.*?\\[(\\d+)\\].*?$";
System.out.println(str.replaceAll(reg, "$1"));
yejinson 2009-06-12
  • 打赏
  • 举报
回复
补充:想要0就这样
Pattern p = Pattern.compile("\\[[[1-9]+(\\d)*|0]\\]");
yejinson 2009-06-12
  • 打赏
  • 举报
回复
随便写下
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));
}
baetg 2009-06-12
  • 打赏
  • 举报
回复
http://edu.yesky.com/edupxpt/18/2143018.shtml

Java正则表达式详解
shibenjie 2009-06-12
  • 打赏
  • 举报
回复

/**
* 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());

}
}
sd5816690 2009-06-12
  • 打赏
  • 举报
回复

String str = "BTSAE[3][REAL_REAK_BTSAE_404]";
String reg = "^.*?\\[([^\\[\\]]*)\\].*?$";
System.out.println(str.replaceAll(reg, "$1"));
sachmx1234 2009-06-12
  • 打赏
  • 举报
回复
搞求不懂..
撒子是正则表达式?
要学的东西还多哦..

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧