java求一个正则表达式

一般般帅的男人 2012-12-28 09:17:18
一个变量如下:
String str="1,232,21,12212,12";
要求匹配:
输入一个参数:如2,匹配1,232如果是3则匹配1,232,21
...全文
227 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
suciver 2013-01-04
  • 打赏
  • 举报
回复
引用 2 楼 abstruct 的回复:
这个没设么好用正则的,如果一定要用那需要是动态的正则了 Java code ? 12345678910 public static void main(String[] args) throws Exception { String str="1,232,21,12212,12"; int i = 3; Matcher m = Pattern……
给力!
meichen8050753 2013-01-04
  • 打赏
  • 举报
回复
这个用的了正则么?直接字符串分割就成了。
感同深受 2013-01-03
  • 打赏
  • 举报
回复
这个要用正则吗?

String[] temp = str.split(",");
String value="";
for(int i=0;i<n;i++){//n是你输入的
if(i!=0){
value+=",";
}
value+=temp[i];
}
...
若鱼1919 2012-12-29
  • 打赏
  • 举报
回复
引用 2 楼 abstruct 的回复:
这个没设么好用正则的,如果一定要用那需要是动态的正则了 Java code?123456789public static void main(String[] args) throws Exception { String str="1,232,21,12212,12"; int i = 3; Matcher m = Pattern.compile("……
nice!
ceclinux 2012-12-28
  • 打赏
  • 举报
回复
引用 2 楼 abstruct 的回复:
这个没设么好用正则的,如果一定要用那需要是动态的正则了 Java code?123456789public static void main(String[] args) throws Exception { String str="1,232,21,12212,12"; int i = 3; Matcher m = Pattern.compile("……
+1
devil_may_care 2012-12-28
  • 打赏
  • 举报
回复
用StringTokenizer貌似就可以啊
huntor 2012-12-28
  • 打赏
  • 举报
回复
举的例子的规律是使用逗号分割后,取前2,3项。
ceclinux 2012-12-28
  • 打赏
  • 举报
回复
引用 楼主 heLove 的回复:
一个变量如下: String str="1,232,21,12212,12"; 要求匹配: 输入一个参数:如2,匹配1,232如果是3则匹配1,232,21
多举几个例子
安特矮油 2012-12-28
  • 打赏
  • 举报
回复
这个没设么好用正则的,如果一定要用那需要是动态的正则了

	public static void main(String[] args) throws Exception {
		String str="1,232,21,12212,12";
		int i = 3;
		Matcher m = Pattern.compile("(,?\\d+){" + i + "}").matcher(str);
		if(m.find()){
			String s = m.group();
			System.out.println(s);
		}
	}
xtfgy2012 2012-12-28
  • 打赏
  • 举报
回复
还是没看出匹配规律,难道是输入的数字乘以2,不可能这么简单吧

62,615

社区成员

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

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