各位大神,求指教正则表达式问题

XDLK2 2015-03-20 12:42:49
一串字符串:{第1-17周|单周}
写一个正则表达式,提取出1,17和单周这三个数据~
...全文
145 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
XDLK2 2015-03-21
  • 打赏
  • 举报
回复
哈哈~终于解决了,谢谢了~
chenjing0320 2015-03-21
  • 打赏
  • 举报
回复
把需求说清楚了啊 [code=java][/ public static List<String> getText(String s) { Pattern p = Pattern.compile("第(\\d+)(\\-(\\d+))?周|(单周)"); Matcher m = p.matcher(s); List<String> result = new ArrayList<String>(); while (m.find()) { if(m.group(1) != null) result.add(m.group(1)); if(m.group(3) != null) result.add(m.group(3)); if(m.group(4) != null) result.add(m.group(4)); } return result; } public static void main(String[] args) { System.out.println(getText("周二第1,2节{第1-17周|单周}")); }code]
XDLK2 2015-03-20
  • 打赏
  • 举报
回复
其实要的是从”周二第1,2节{第1-17周|单周}”中,取出1,17和单周(单周不一定每条数据都有.)
chenjing0320 2015-03-20
  • 打赏
  • 举报
回复
	public static List<String> getText(String s) {
		Pattern p = Pattern.compile("\\d+|单周");
		Matcher m = p.matcher(s);
		List<String> result = new ArrayList<String>();
		while (m.find()) {
			result.add(m.group());
		}
		return result;
	}
	
	public static void main(String[] args) {
		
		System.out.println(getText("{第1-17周|单周}"));
		
	}

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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