在regex中,匹配字符串的末尾或特定字符

时间:2022-09-19 17:57:53

I have a string. The end is different, such as index.php?test=1&list=UL or index.php?list=UL&more=1. The one thing I'm looking for is &list=.

我有一个字符串。结尾不同,比如index.php?测试= 1列表= UL或index . php ? = UL&more = 1列表。我要找的是&list=。

How can I match it, whether it's in the middle of the string or it's at the end? So far I've got [&|\?]list=.*?([&|$]), but the ([&|$]) part doesn't actually work; I'm trying to use that to match either & or the end of the string, but the end of the string part doesn't work, so this pattern matches the second example but not the first.

如何匹配它,不管是在弦的中间还是在末端?到目前为止我有=[& | \ ?]列表。* ?((& | $)),但((& | $))的部分不工作;我试着用它来匹配&或字符串的结尾,但是字符串部分的结尾不起作用,所以这个模式匹配第二个例子,但不匹配第一个。

1 个解决方案

#1


36  

Use:

使用:

/(&|\?)list=.*?(&|$)/

Note that when you use a bracket expression, every character within it (with some exceptions) is going to be interpreted literally. In other words, [&|$] matches the characters &, |, and $.

注意,当您使用方括号表达式时,其中的每个字符(除了某些例外)都将按字面意义进行解释。换句话说,[&|$]匹配字符&、|和$。

#1


36  

Use:

使用:

/(&|\?)list=.*?(&|$)/

Note that when you use a bracket expression, every character within it (with some exceptions) is going to be interpreted literally. In other words, [&|$] matches the characters &, |, and $.

注意,当您使用方括号表达式时,其中的每个字符(除了某些例外)都将按字面意义进行解释。换句话说,[&|$]匹配字符&、|和$。