![](/img/trans.png)
[英]Regex to remove a non-alphanumeric character at the end of a string which also appears else where in the string
[英]Get string between a known string and any non-alphanumeric character
我有这段代码,可以让我获取“ Global”之间的字符串。 和“”。
private string getGlobalVariableName(string text)
{
int pFrom = text.IndexOf("Global.") + "Global.".Length;
int pTo = text.LastIndexOf(" ");
string name = text.Substring(pFrom, pTo - pFrom);
return name;
}
我想对其进行修改,以使其获得“全局”之间的字符串。 以及任何非字母数字字符。 我该怎么办?
例:
这对于我现在拥有的是正确的
getGlobalVariableName(“ foo Global.bar1 foobar”)==“ bar1”
这就是我想要做的
getGlobalVariableName(“ foo Global.bar1> foobar”)==“ bar1”
您可以使用正则表达式...
string input = "Global.bar1>foobar";
var output = Regex.Match(input, @"Global.([\w]+)").Groups[1].Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.