正则表达式:从查询字符串中的逗号分隔列表中删除确切的数字

时间:2021-10-05 00:18:13

I'm passing a comma separated list of numbers as value in a query string. For example, I have the following query string:

我在查询字符串中将逗号分隔的数字列表作为值传递。例如,我有以下查询字符串:

?view=list&cat=1,8,18&sort=asc

Now I want to remove "8" from the "cat" variable. So far I have this, to only match the value of "cat"

现在我想从“cat”变量中删除“8”。到目前为止,我有这个,只匹配“猫”的价值

(?<=cat=)([^&]*)

This will get me "1,8,18". Now I only want to select the exact digit "8", is that possible? I know it would be easier to convert the comma separated list into an array, but in this specific case that's not possible.

这将让我“1,8,18”。现在我只想选择确切的数字“8”,这可能吗?我知道将逗号分隔列表转换为数组会更容易,但在这种特殊情况下,这是不可能的。

Edit: The regex must only match the number in the "cat" var and must work in comma separated list or as a single value. So the following test strings:

编辑:正则表达式必须只匹配“cat”var中的数字,并且必须在逗号分隔列表中工作或作为单个值。所以以下测试字符串:

?view=list&cat=1,8,18&sort=asc
?view=list&cat=8&sort=asc
?view=list&cat=1,8,18&sort=asc&someOtherVar=8

They all must only match the single number "8" (not the last 8, because it's not preceded by "cat=")

它们都必须只匹配单个数字“8”(不是最后8个,因为它之前没有“cat =”)

3 个解决方案

#1


1  

Without lookbehind:

(cat=(?:\b[^8]\d*\b)*?),?8(&)|8,

and replace with capturing groups 1 & 2

并替换为捕获组1和2

Now testing against your added examples above, the regex replace returns:

现在测试上面添加的示例,正​​则表达式替换返回:

?view=list&cat=1,18&sort=asc
?view=list&cat=&sort=asc
?view=list&cat=1,18&sort=asc&someOtherVar=8

#2


0  

I am not very sure of your requirements, this regex \bcat=(?:\d+,)*(8(?!\d),?) will capture in group number 1 the number 8, (the coma if exists) (which you can change it by a variable number).

我不太确定你的要求,这个正则表达式\ bcat =(?:\ d +,)*(8(?!\ d),?)将在组号1中捕获数字8,(如果存在昏迷)(您可以通过变量编号更改它)。

You can test it with different input here : http://regex101.com/r/uM0bS7

您可以在此处使用不同的输入进行测试:http://regex101.com/r/uM0bS7

It will match:

它会匹配:

  • ?view=list&cat=3,1,8,18&sort=asc
  • ?view=list&cat=8,18&sort=asc
  • ?view=list&cat=8&sort=asc
  • ?view=list&cat=3,1,8,18,8&sort=asc
  • ?view=list&cat=3,1,8&sort=asc

If you have the value 8 twice it will match the last one, if you want to match the first one you only need to make the non capturing group (?:\d+,)* lazy like this : \bcat=(?:\d+,)*?(8(?!\d),?)

如果你的值是8,它将匹配最后一个,如果你想匹配第一个,你只需要使非捕获组(?:\ d +,)*懒如此:\ bcat =(?:\ d +)*?(8(?!\ d),?)

#3


0  

I believe this regex:

我相信这个正则表达式:

(?<=cat=)(?:[^,]*,)*(8)(?:\W|$)

should give 8 in matched group # 1 to you.

应该给你匹配组#1 8。

Live Demo: http://www.rubular.com/r/2tp0HiYEaY

#1


1  

Without lookbehind:

(cat=(?:\b[^8]\d*\b)*?),?8(&)|8,

and replace with capturing groups 1 & 2

并替换为捕获组1和2

Now testing against your added examples above, the regex replace returns:

现在测试上面添加的示例,正​​则表达式替换返回:

?view=list&cat=1,18&sort=asc
?view=list&cat=&sort=asc
?view=list&cat=1,18&sort=asc&someOtherVar=8

#2


0  

I am not very sure of your requirements, this regex \bcat=(?:\d+,)*(8(?!\d),?) will capture in group number 1 the number 8, (the coma if exists) (which you can change it by a variable number).

我不太确定你的要求,这个正则表达式\ bcat =(?:\ d +,)*(8(?!\ d),?)将在组号1中捕获数字8,(如果存在昏迷)(您可以通过变量编号更改它)。

You can test it with different input here : http://regex101.com/r/uM0bS7

您可以在此处使用不同的输入进行测试:http://regex101.com/r/uM0bS7

It will match:

它会匹配:

  • ?view=list&cat=3,1,8,18&sort=asc
  • ?view=list&cat=8,18&sort=asc
  • ?view=list&cat=8&sort=asc
  • ?view=list&cat=3,1,8,18,8&sort=asc
  • ?view=list&cat=3,1,8&sort=asc

If you have the value 8 twice it will match the last one, if you want to match the first one you only need to make the non capturing group (?:\d+,)* lazy like this : \bcat=(?:\d+,)*?(8(?!\d),?)

如果你的值是8,它将匹配最后一个,如果你想匹配第一个,你只需要使非捕获组(?:\ d +,)*懒如此:\ bcat =(?:\ d +)*?(8(?!\ d),?)

#3


0  

I believe this regex:

我相信这个正则表达式:

(?<=cat=)(?:[^,]*,)*(8)(?:\W|$)

should give 8 in matched group # 1 to you.

应该给你匹配组#1 8。

Live Demo: http://www.rubular.com/r/2tp0HiYEaY