在关键字之后和问号之前匹配的正则表达式 [英] Regex that wil match after keyword and before question mark

查看:413
本文介绍了在关键字之后和问号之前匹配的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正用以下测试字符串卡在regex101上:

I'm getting stuck on regex101 with the following test string:

https://localhost:8443/site/recipes/recepten-zoeken/menugang:hoofdgerecht/soort:italiaans/seizoen:winter?Nrpp = 24

我正在尝试匹配介于两者之间的所有内容:recepten-zoeken/和?

I'm trying to match everything in between: recepten-zoeken/ and ?

由于我是新手,正在编写正则表达式,因此我目前的尝试无济于事.任何人想要加入吗?

My current tries lead me nowhere as i'm very novice writing regexes. Anyone wanting to chip in?

这是我目前拥有的:

.(?=([[^/recepten-zoeken/]?)* $)

.(?=([^/recepten-zoeken/]?)*$)

推荐答案

使用捕获组.(()中的括号表示捕获组)

Use capturing group.(() brackets indicates capturing group)

捕获到最后一个问号

尝试此正则表达式\/recepten-zoeken\/(.*)\?

说明:-

  • \/匹配正斜杠
  • recepten-zoeken字面上匹配
  • \/匹配正斜杠
  • (.*)捕获组中的所有值(换行符除外),直到字符串中的最后一个问号(这将包含您的值)
  • \?字面上匹配
  • \/ Match forward slash
  • recepten-zoeken Match literally
  • \/ Match forward slash
  • (.*) Capture in a group all value(except new line) upto last question mark in string(This will contain your value)
  • \? Match literally

//-------------------------select upto last ? -----------------------------

str = "https://localhost:8443/site/recipes/recepten-zoeken/menugang:hoofdgerecht/soort:italiaans/seizoen:winter?Nrpp=24";
var myRegexp = /\/recepten-zoeken\/(.*)\?/;
console.log(myRegexp.exec(str)[1]);

捕获第一个问号

尝试此正则表达式\/recepten-zoeken\/([^?]*)\?.

说明:-

  • \/匹配正斜杠
  • recepten-zoeken字面上匹配
  • \/匹配正斜杠
  • ([^?]*)将所有值(?除外)捕获到字符串中的第一个问号(其中将包含您的值)(此处[^?]表示匹配除?以外的任何字符)
  • \?字面上匹配
  • \/ Match forward slash
  • recepten-zoeken Match literally
  • \/ Match forward slash
  • ([^?]*) Capture in a group all value(except ?) upto first question mark in string(This will contain your value)(here [^?] means match any character except ?)
  • \? Match literally

//-------------------------select upto first ? -----------------------------

var str = "https://localhost:8443/site/recipes/recepten-zoeken/menugang:hoofdgerecht/soort:italiaans/seizoen?:winter?Nrpp=24";
var myRegexp = /\/recepten-zoeken\/([^?]*)\?/;
console.log(myRegexp.exec(str)[1]);

这篇关于在关键字之后和问号之前匹配的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆