简体   繁体   English

获取已知字符串和任何非字母数字字符之间的字符串

[英]Get string between a known string and any non-alphanumeric character

I have this code which allows me to get the string between "Global." 我有这段代码,可以让我获取“ Global”之间的字符串。 and " ". 和“”。

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;
    }

I want to modify it so that it gets the string between "Global." 我想对其进行修改,以使其获得“全局”之间的字符串。 and any non-alphanumeric character. 以及任何非字母数字字符。 How could I do this? 我该怎么办?

Example: 例:

this is true for what I have now 这对于我现在拥有的是正确的

getGlobalVariableName(" foo Global.bar1 foobar") == "bar1" getGlobalVariableName(“ foo Global.bar1 foobar”)==“ bar1”

this is what I want to be able to do 这就是我想要做的

getGlobalVariableName(" foo Global.bar1>foobar") == "bar1" getGlobalVariableName(“ foo Global.bar1> foobar”)==“ bar1”

You can use Regex... 您可以使用正则表达式...

string input = "Global.bar1>foobar";
var output = Regex.Match(input, @"Global.([\w]+)").Groups[1].Value;

暂无
暂无

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

相关问题 正则表达式删除字符串末尾的非字母数字字符,该字符也出现在字符串中的其他位置 - Regex to remove a non-alphanumeric character at the end of a string which also appears else where in the string Windows形成文本框或c#字符串本机方法,用于检查非字母数字字符 - Windows forms textbox or c# string native method for checking non-alphanumeric character 检查字符串是否包含非字母数字(下划线除外) - Check if string contains non-alphanumeric except underscore 仅从字符串的开头和结尾删除非字母数字字符 - Remove non-alphanumeric characters from start and end of string only 获取第二个非字母数字的索引 - Get index of second non-alphanumeric 正则表达式为1或2位数,可选的非字母数字,2个已知的alphas - Regex for 1 or 2 digits, optional non-alphanumeric, 2 known alphas 如何使用正则表达式从输入字符串中提取所有非字母数字字符? - How can I extract all non-alphanumeric characters from an input string using Regular Expressions? 正则表达式考虑$以外的非字母数字字符 - Regex to consider non-alphanumeric character other than $ 字符串包含非字母数字字符(如中文或日语)时的对齐问题 - Alignment issue when string contains non-alphanumeric characters like Chinese or Japanese 如何从字符串中去除非字母数字字符(包括空格)? - How do I strip non-alphanumeric characters (including spaces) from a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM