这个正则表达式与最后两行不匹配,但理论上它应该

时间:2018-04-26 11:04:01

标签: javascript regex

我有以下文字我正在尝试匹配

[id] [int] IDENTITY(1,1) NOT NULL,
    [name] [nvarchar](255) NOT NULL,
    [type] [nvarchar](255) NOT NULL,
    [number_of_persons] [int] NOT NULL,
    [number_of_suitcases] [int] NOT NULL,
    [created_at] [datetime2](3) NOT NULL,
    [updated_at] [datetime2](3) NOT NULL,
    [description] [text] NULL,
    [number_of_children] [int] NULL,
    [number_of_small_suitcases] [int] NULL,
    [code] [nvarchar](255) NULL,
    [sort_order] [int] NULL,
    [default_number_persons] [int] NULL,
    [margin_fixed] [nvarchar](10) NULL,
    [margin_percentage] [nvarchar](10) NULL,
    [car_type_id] [int] NULL,
    [recommended] [bit] NULL
)

我正在使用以下正则表达式匹配逗号后跟换行符。

\,\s?(?![^\(]*\))

哪个应该如下工作:

Explanation of regex

但是当我使用它时,它在我最后3行的文字上失败了

failing matches in regex

我在javascript中使用它来拆分上面的字符串。

在此示例中,正则表达式失败的原因是什么?在这个样本之前它完美无缺。

string.split(/\,\s?(?![^\(]*\))/);

修改

我忘了正则表达式必须遵守的另一个约束,以防你想提出修复。 这个正则表达式也必须否定约束内的文本,将它全部推入一个大字符串,但匹配前两个。这是目前正则表达式的作用。

在下面的代码段中查看testcase的输出,我的意思是。

[car_id] [int] NOT NULL,
    [car_sales_combo_id] [int] NOT NULL,
 CONSTRAINT [PK_exitcontrol_carmanager_car_to_combo] PRIMARY KEY CLUSTERED 
(
    [car_id] ASC,
    [car_sales_combo_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)

console.log("Testing regex fail");
console.log(document.getElementById('text').value.split(/\,\s?(?![^\(]*\))/));
console.log("Testing testcase it will also have to handle. The third array entry should be one big text blob");
console.log(document.getElementById('testcase').value.split(/\,\s?(?![^\(]*\))/));
<textarea id="text">
  [id] [int] IDENTITY(1,1) NOT NULL,
	[name] [nvarchar](255) NOT NULL,
	[type] [nvarchar](255) NOT NULL,
	[number_of_persons] [int] NOT NULL,
	[number_of_suitcases] [int] NOT NULL,
	[created_at] [datetime2](3) NOT NULL,
	[updated_at] [datetime2](3) NOT NULL,
	[description] [text] NULL,
	[number_of_children] [int] NULL,
	[number_of_small_suitcases] [int] NULL,
	[code] [nvarchar](255) NULL,
	[sort_order] [int] NULL,
	[default_number_persons] [int] NULL,
	[margin_fixed] [nvarchar](10) NULL,
	[margin_percentage] [nvarchar](10) NULL,
	[car_type_id] [int] NULL,
	[recommended] [bit] NULL
)
</textarea>
<textarea id="testcase">
[car_id] [int] NOT NULL,
	[car_sales_combo_id] [int] NOT NULL,
 CONSTRAINT [PK_exitcontrol_carmanager_car_to_combo] PRIMARY KEY CLUSTERED 
(
	[car_id] ASC,
	[car_sales_combo_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
</textarea>

3 个答案:

答案 0 :(得分:1)

此RegEx会捕获每行的最后一个“,”。

 (,\n)

答案 1 :(得分:1)

你的正则表达式的作用是匹配一个逗号,可选地后跟一个空格,不是,然后是一个没有(以{{结尾的字符串的字符串1}}

最后一行

)

负面展望失败,因为这些行中的所有逗号确实后跟一个没有[margin_percentage] [nvarchar](10) NULL, [car_type_id] [int] NULL, [recommended] [bit] NULL ( 结尾的字符串,因为)int类型没有长度(没有括号)。

答案 2 :(得分:0)

我认为你可以通过简单地放弃对(字符的否定来实现你想做的事情

/\,\s(?![\(]*\))/g

而不是

/\,\s(?![^\(]*\))/g

点击此处:https://regex101.com/r/VfQIJC/7

这样一来,您就会检查没有紧跟任意数量的(后跟)的逗号。