排除带有特定数字的输入字符串的正则表达式

Regex that excludes input strings with specific numbers

本文关键字:输入 字符串 正则表达式 数字 排除      更新时间:2023-09-26

例如,我有以下4个输入:

{168,3816,2012,[04,14]}
{168,38087,2012,[14,32]}
{168,37955,2012,[14,32]}
{168,33409,2011,[14]}

我想要排除输入4,因为它在第一个逗号后面有数字33409,所以我的最终结果将是:

{168,3816,2012,[04,14]}
{168,38087,2012,[14,32]}
{168,37955,2012,[14,32]}

我想出了以下正则表达式.*,(?!(33409))'d{5},.*,但它未能包括第一个输入:{168,3816,2012,[04,14]},这是因为我指定了范围d{5}。使用'd+或'd{0,5}不起作用。

我希望我的正则表达式能够排除多个数字,例如输入数字33409或3816后的第一个逗号。所以最终结果将是:

{168,38087,2012,[14,32]}
{168,37955,2012,[14,32]}

感谢大家的反馈。我意识到我需要给出问题的更多细节才能得到正确的答案。下面是对这个问题的详细解释。我有一个带有元数据的图书标题列表。元数据由ItemClass,Itemkey,ItemPublicationyear and ItemCategory组成。书名是返回格式如下:

booktitle {itemclass,itemkey,itempublicationyear,[itemcategory]} 
//ItemCategory can have mult. values

REGEX形式的过滤器应用于{}中的元数据,以向用户返回正确的标题。

图书标题示例列表如下所示:

Business Liability Insurance  2011-12 {168,326,2011,[14,32]}   //itemcategory with mult. values
Insurance Regulation 2013 {168,37955,2012,[14,32]}
Financial Institutions  {168,33734,2011,[14]} //itemcategory with just one value
Insider Trading Law {168,32645,2011,[04,14]}
Business Liability Insurance {168,32647,2011,[14,32,45]}

用户请求可以由一个或多个过滤器组成。例如,用户可以请求属于itemclass 102,33,168的图书标题列表,因此REGEX将像这样:

(102|33|168),(.*),(.*),(.*) //I didn't use (102|33|168),(.*) because I want 
the format of the expression to be ItemClass,ItemKey,ItemPublicationYear,ItemCategory

Another request with filter  ItemCategory= 14 will look like this: (.*),(.*),(.*),'[((('d+),)*(14)(,'d+)*)']
If user applied both the ItemClass and ItemCategory filters the regex will look like this:
(102|33|168),(.*),(.*),'[((('d+),)*(14)(,'d+)*)']

我让它对所有过滤器都起作用。当用户想要使用过滤器来排除特定的图书通过应用itemkey过滤器,以便用户可以请求除项目键为32647、326的图书外的所有图书标题

Itemclasses     Itemkey         ItemPublicationYear    ItemCategory     Regex
------------------------------------------------------------------------------
    All         All             All                     All             (.*),(.*)(.*),(.*)      //no filters applied
    110,112     All             All                     All             (110|112),(.*),(.*),(.*)
    All         38524           All                     All             (.*),(38524),(.*),(.*)
    All         All             2004-2014               All             (.*),(.*),2(0(0([4-9])|1([0-4]))),(.*)
    All         All             All                    24,21,27         (.*),(.*),(.*),'[((('d+),)*(24|21|27)(,'d+)*)']
    110,112     38524           All                    24,21,27         (110|112),(38524),(.*),'[((('d+),)*(24|21|27)(,'d+)*)']

您想要的最终结果没有得到适当的解释?
上面的输入都是字符串吗?

"{168, 3816, 2012,(04 14)}"
"{168年、38087年、2012年,[14日32]}"
"{168年、37955年、2012年,[14日32]}"
"{168年、33409年、2011年,[14]}"
你能回答以下问题吗?
你到底想要匹配什么?
要检查第一个逗号后的第一个数字是否不等于33409吗?
"我希望我的正则表达式能够排除多个数字"=>你的意思是这里的多个数字?

然而,从我的理解试试下面的regex

/{' d {3 }',(?:!(?: 33409 | 3816)) ' ' d {4}, [' d + ' ' d +]}

这将尝试匹配

{3 digit no, not equal to 33409 or 3816, 4digit no, [any digit no, any digit no]}