C# 如何提取位于括号(圆括号)之间的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/378415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract text that lies between parentheses (round brackets)?
提问by
I have a string User name (sales)
and I want to extract the text between the brackets, how would I do this?
我有一个字符串User name (sales)
,我想提取括号之间的文本,我该怎么做?
I suspect sub-string but I can't work out how to read until the closing bracket, the length of text will vary.
我怀疑是子字符串,但在结束括号之前我无法弄清楚如何阅读,文本的长度会有所不同。
回答by chills42
A regex maybe? I think this would work...
也许是正则表达式?我认为这会奏效...
\(([a-z]+?)\)
回答by Jennifer
Regular expressions might be the best tool here. If you are not famililar with them, I recommend you install Expresso- a great little regex tool.
正则表达式可能是这里最好的工具。如果您不熟悉它们,我建议您安装Expresso- 一个很棒的小正则表达式工具。
Something like:
就像是:
Regex regex = new Regex("\((?<TextInsideBrackets>\w+)\)");
string incomingValue = "Username (sales)";
string insideBrackets = null;
Match match = regex.Match(incomingValue);
if(match.Success)
{
insideBrackets = match.Groups["TextInsideBrackets"].Value;
}
回答by Diadistis
A very simple way to do it is by using regular expressions:
一个非常简单的方法是使用正则表达式:
Regex.Match("User name (sales)", @"\(([^)]*)\)").Groups[1].Value
As a response to the (very funny) comment, here's the same Regex with some explanation:
作为对(非常有趣的)评论的回应,这里有相同的正则表达式,并附有一些解释:
\( # Escaped parenthesis, means "starts with a '(' character"
( # Parentheses in a regex mean "put (capture) the stuff
# in between into the Groups array"
[^)] # Any character that is not a ')' character
* # Zero or more occurrences of the aforementioned "non ')' char"
) # Close the capturing group
\) # "Ends with a ')' character"
回答by Nick Allen
string input = "User name (sales)";
string output = input.Substring(input.IndexOf('(') + 1, input.IndexOf(')') - input.IndexOf('(') - 1);
回答by Ross Goddard
Assuming that you only have one pair of parenthesis.
假设你只有一对括号。
string s = "User name (sales)";
int start = s.IndexOf("(") + 1;
int end = s.IndexOf(")", start);
string result = s.Substring(start, end - start);
回答by Will Dean
Use a Regular Expression:
使用正则表达式:
string test = "(test)";
string word = Regex.Match(test, @"\((\w+)\)").Groups[1].Value;
Console.WriteLine(word);
回答by Rockcoder
input.Remove(input.IndexOf(')')).Substring(input.IndexOf('(') + 1);
回答by inspite
The regex
method is superior I think, but if you wanted to use the humble substring
这个regex
方法我认为是优越的,但如果你想使用谦虚substring
string input= "my name is (Jayne C)";
int start = input.IndexOf("(");
int stop = input.IndexOf(")");
string output = input.Substring(start+1, stop - start - 1);
or
或者
string input = "my name is (Jayne C)";
string output = input.Substring(input.IndexOf("(") +1, input.IndexOf(")")- input.IndexOf("(")- 1);
回答by artfulhacker
Use this function:
使用这个功能:
public string GetSubstringByString(string a, string b, string c)
{
return c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b) - c.IndexOf(a) - a.Length));
}
and here is the usage:
这是用法:
GetSubstringByString("(", ")", "User name (sales)")
and the output would be:
输出将是:
sales
回答by Jelly Ama
If you wish to stay away from regular expressions, the simplest way I can think of is:
如果你想远离正则表达式,我能想到的最简单的方法是:
string input = "User name (sales)";
string output = input.Split('(', ')')[1];