正则表达式只匹配到第一次出现类匹配 [英] Regex to match only till first occurence of class match

查看:101
本文介绍了正则表达式只匹配到第一次出现类匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找构建一个正则表达式来帮助我识别匹配的第一次出现.

I'm looking to construct a regex which will help me identify the first occurrence of a match.

我当前的正则表达式是 "(.*)[Cc][Aa][Ss][Ee][^a-zA-Z\\d]*(\\d\\d*)[^a-zA-Z\\d]*(.*)"

My current regex is "(.*)[Cc][Aa][Ss][Ee][^a-zA-Z\\d]*(\\d\\d*)[^a-zA-Z\\d]*(.*)"

我想要做的是查找输入字符串是否包含单词case"(不区分大小写),后跟任意数量的特殊字符,后跟一个数字;我想检索文本的 3 部分.假设我的输入字符串是 "RE: FW: case:-1234: there is some description"使用这个正则表达式,我可以检索,"RE: FW:", "1234", "there is some description".

What I am trying to do is to find if the input string contains the word "case" (case insensitive), followed by any number of special characters, followed by a number; I want to retrieve 3 parts of the text. Say my input string is "RE: FW: case:-1234: there is some description" Using this regex, I am able to retrieve, "RE: FW: ", "1234", "there is some description".

这很好,但如果我的输入字符串是RE:FW:case:-1234:这是关于案例789重新开放"然后我的正则表达式返回,"RE: FW: case:-1234: This is in reference to", "789", "reopening".

This is fine, but if my input string is "RE: FW: case:-1234: This is in reference to case 789 reopening" Then my regex returns, "RE: FW: case:-1234: This is in reference to", "789", "reopening".

我想得到的是"RE: FW:", "1234", "这是关于案例789重新开放".

What I would like to get is "RE: FW: ", "1234", "This is in reference to case 789 reopening".

我是正则表达式的新手,非常感谢您的帮助.

I am a newbie with regex, so any help is much appreciated.

注意:我正在开发基于 Java 的工具,因此与 Java 兼容的正则表达式会很好.

Note: I am working on a java based tool, so java compatible regex would be nice.

推荐答案

您的正则表达式是否必须匹配整个字符串(即它是否使用 matches)?如果不是(或者如果您可以选择使用 find 代替),只需删除 (.*),因为那是将您的匹配推回去的原因:

Does your regex have to match the entire string (i.e. does it use matches)? If not (or if you can choose to use find instead) simply remove the (.*), because that's what pushes your match back:

[Cc][Aa][Ss][Ee][^a-zA-Z\\d]*(\\d\\d*)[^a-zA-Z\\d]*

否则,使前导重复非贪婪;

Otherwise, make the leading repetition non-greedy;

(.*?)[Cc][Aa][Ss][Ee][^a-zA-Z\\d]*(\\d\\d*)[^a-zA-Z\\d]*(.*)

顺便说一下,您可以使用不区分大小写的匹配来简化此操作.如果您无法在您的工具中激活它,您可以在正则表达式中内联:

By the way, you can simplify this, using case-insensitive matching. If you cannot activate it in your tool, you can do it inline in the regex:

(?i)(.*?)case[^a-z\\d]*(\\d+)[^a-z\\d]*(.*)

请注意,我还简化了数字.+ 表示出现 1 次或多次.

Note that I also simplified the number. + means 1 or more occurrence.

这篇关于正则表达式只匹配到第一次出现类匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆