在两个参数之间查找多个字符串。

huangapple go评论104阅读模式
英文:

Find multiple strings between two arguments

问题

我正在尝试创建一个函数,该函数将获取两个参数(rewriteredirect)之间的字符串。我无法理解它。

我有一个看起来像这样的字符串:

add_header X-Robots-Tag "noindex, follow" always; rewrite ^/en/about/Administration/index.aspx /en/about/more-about/administration redirect; rewrite ^/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration redirect; rewrite ^/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields redirect; rewrite ^/en/about/For-companies/index.aspx /en/about/more-about/for-companies redirect; rewrite ^/en/about/contact-information/index.aspx /en/about/more-about/contact-information redirect; rewrite ^/en/about/index.aspx /nl/over redirect;

我想要以下输出:

/en/about/Administration/index.aspx /en/about/more-about/administration
 
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration 

/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields  

/en/about/For-companies/index.aspx /en/about/more-about/for-companies

/en/about/contact-information/index.aspx /en/about/more-about/contact-information

/en/about/index.aspx /nl/over

获取两个参数之间所有字符串的正确正则表达式或方法是什么?

英文:

I'm trying to make a function that will get a string between two arguments (rewrite and redirect). I can't wrap my head around it.

I have a string that looks like this:

add_header X-Robots-Tag "noindex, follow" always; rewrite ^/en/about/Administration/index.aspx /en/about/more-about/administration redirect; rewrite ^/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration redirect; rewrite ^/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields redirect; rewrite ^/en/about/For-companies/index.aspx /en/about/more-about/for-companies redirect; rewrite ^/en/about/contact-information/index.aspx /en/about/more-about/contact-information redirect; rewrite ^/en/about/index.aspx /nl/over redirect;

And I want the following output:

/en/about/Administration/index.aspx /en/about/more-about/administration
 
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration 

/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields  

/en/about/For-companies/index.aspx /en/about/more-about/for-companies

/en/about/contact-information/index.aspx /en/about/more-about/contact-information

/en/about/index.aspx /nl/over

What is the proper regex or method to get all strings between the two arguments?

答案1

得分: 0

尝试:

\brewrite \^(.*?)\s+redirect;

在线演示:点击这里


  • \brewrite \^ - 字面上是在左边的单词边界和右边的空格后面跟着字面上的 ^
  • (.*?) - 匹配(懒惰模式)0个或多个字符;
  • \s+redirect; - 字面上是在左边的1个或多个空白字符和右边的分号后面跟着字面上的 redirect

查看在线的GO演示:点击这里,它将打印出:

/en/about/Administration/index.aspx /en/about/more-about/administration
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration
/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields
/en/about/For-companies/index.aspx /en/about/more-about/for-companies
/en/about/contact-information/index.aspx /en/about/more-about/contact-information
/en/about/index.aspx /nl/over
英文:

Try:

\brewrite \^(.*?)\s+redirect;

See an online demo


  • \brewrite \^ - literally 'rewrite' between a word-boundary on the left and a space followed by literal '^' on the right;
  • (.*?) - Match (lazy) 0+ characters;
  • \s+redirect; - Literally 'redirect' between 1+ whitespace characters on the left and a semi-colon on the right.

See an online GO demo which will print:

/en/about/Administration/index.aspx /en/about/more-about/administration
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration
/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields
/en/about/For-companies/index.aspx /en/about/more-about/for-companies
/en/about/contact-information/index.aspx /en/about/more-about/contact-information
/en/about/index.aspx /nl/over

huangapple
  • 本文由 发表于 2022年10月13日 20:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/74055599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定