简体   繁体   English

返回两个字符之间的值?

[英]Return value between two characters?

If we use the example string:如果我们使用示例字符串:

localhost:12345/MATCH_THIS_PART/more_strings

How would I return the value MATCH_THIS_PART and set it as the value of returnString .我将如何返回值MATCH_THIS_PART并将其设置为returnString的值。 I think the problem can be solved using regex, and this is what I think would work:我认为可以使用正则表达式解决问题,这就是我认为可行的方法:

string returnString = Regex.Match(Request.UrlReferrer.ToString(), @"not sure what goes here");

So basically, what goes in the part where I put所以基本上,我放的那部分是什么

"not sure what goes here" “不知道这里发生了什么”

There are several options.有几种选择。 String splitting is the easiest one:字符串拆分是最简单的一种:

string part = Request.UrlReferrer.ToString().Split('/')[1]; // gets the second item

Or use this regular expression and get the first capture:或者使用这个正则表达式并获取第一个捕获:

.*?\/(.*?)\/.*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM