正则表达式 - 抓取两个字符之间的字符,但不包括它们

时间:2016-03-30 23:24:04

标签: ruby-on-rails regex

我正试图在'='和'&'之间的字符串中抓取rsession变量在一个字符串中。

我可以使用/rsession(\=.+?\&)/

但我该如何做,所以输出不包括'='和'&'?

字符串是: "app=1334300&rsession=0806343413_1:5bc6a3c80271826a1c0016c1520d3&token=a6caacf7edfbd9383429e30a1adfadf385208985ad&redirectReq=true"

2 个答案:

答案 0 :(得分:1)

您可以使用此正则表达式:

/(?<=rsession=)[^&]*/

说明:

(?<=              # asserts the match at position after
    rsession=     # the literal string `rsession=`
)                 # it is called: positive lookbehind
[^&]              # not `&` character
*                 # as many as possible

希望它有所帮助。

答案 1 :(得分:0)

你可以在这样的组中捕获它。

正则表达式: /rsession=([^&]+)/

<强>解释

  • ([^&]+)将在群组中捕获所有字符,直到满足&为止。使用\1$1来使用已捕获的群组。

<强> Regex101 Demo

相关问题