Java正则表达式:如果结束括号是字符串中的最后一个字符,则匹配
发布时间:2020-12-15 04:16:42 所属栏目:Java 来源:网络整理
导读:我需要一些帮助来保存我的一天(或我的夜晚).我想匹配: 任意数量的数字 用圆括号括起来“()”[括号中除了数字之外别无其他] 如果结束括号“)”是字符串中的最后一个字符. 这是我提出的代码: // this how the text looks,the part I want to match are the d
我需要一些帮助来保存我的一天(或我的夜晚).我想匹配:
>任意数量的数字 这是我提出的代码: // this how the text looks,the part I want to match are the digits in the brackets at the end of it String text = "Some text 45 Some text,text and text (1234)"; String regex = "[no idea how to express this.....]"; // this is where the regex should be Pattern regPat = Pattern.compile(regex); Matcher matcher = regPat.matcher(text); String matchedText = ""; if (matcher.find()) { matchedText = matcher.group(); } 请帮我解决我只能设置匹配任意数字的神奇表达式,但如果它们括在括号中并且位于行尾,请不要帮助我… 谢谢! 解决方法
你可以尝试这个正则表达式:
String regex = "(d+)$"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |