且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用.Net(windowsapplication)提取位于两个(括号)之间的文本字符串

更新时间:2023-02-19 09:03:55

目前尚不清楚两个括号之间是什么意思。你可能不会想到它,但(小)(999)是ano这种匹配,但你可能需要(小)或(999),这被称为懒惰模式。



你可以找到各种各样的匹配使用正则表达式。例如:



It is not clear what do you mean by "between two brackets". You might not think about it, but "(small)(999)" is another kind of match, but you probably need "(small)" or "(999)", which is called a lazy pattern.

You can find all kinds of matches using Regular Expressions. For example:

using System.Text.RegularExpressions;

//...

string sampleInput = "soap(small)(999)";
Regex regex = new Regex(@"\(.*?\)");
MatchCollection matches = regex.Matches(sampleInput);
int count = matches.Count;

//...

string s0 = matches[0].Result; //will give you (small) 
string s1 = matches[1].Result; //will give you (999) 





但如果你使用





But if you use

Regex regex = new Regex(@"\(.*\)");

你只得到一场比赛(小)(999)。



见:

http://msdn.microsoft.com/en-us/library/hs600312.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.text。 regularexpressions.regex.aspx [ ^ ],

http://en.wikipedia.org/wiki/Regular_expression [ ^ ]。



-SA

you will get only one match "(small)(999)".

See:
http://msdn.microsoft.com/en-us/library/hs600312.aspx[^],
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^],
http://en.wikipedia.org/wiki/Regular_expression[^].

—SA


如何''回合:
using System.Text.RegularExpressions;

		
string myString = "soap(small)(999)";
string insideParentheses = Regex.Match(myString, "([0-9][0-9][0-9])").Groups(0).ToString();


string xx = y.Substring(y.LastIndexOf(()+ 1,(y.LastIndexOf()) - y.LastIndexOf( ()) - 1);
string xx = y.Substring(y.LastIndexOf("(") + 1, (y.LastIndexOf(")") - y.LastIndexOf("(")) - 1);