正则表达式仅在类匹配首次出现之前进行匹配 [英] Regex to match only till first occurence of class match
问题描述
我正在寻找一个正则表达式,它将帮助我识别匹配的第一个匹配项.
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: This is in reference to case 789 reopening"
然后我的正则表达式返回"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"
,"This is in reference to case 789 reopening"
.
我是regex的新手,因此非常感谢您的帮助.
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屋!