如何使用.Net(windowsapplication)提取位于两个(括号)之间的文本字符串 [英] How do I extract a string of text that lies between two (brackets) using .Net (windowsapplication)

查看:128
本文介绍了如何使用.Net(windowsapplication)提取位于两个(括号)之间的文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我可以提取一串位于两个之间的文本(括号)

Hi all,


I can extract a string of text that lies between two (brackets)

string input = "soap(999)";
String SID =     input.Remove(input.IndexOf(')')).Substring(input.IndexOf('(') + 1);




output: 999



这很好,但我需要的是




This is fine, But my need is

String input = "soap(small)(999)";



如果我的字符串是这样的意思,我无法从字符串999中检索id





注意:在我的字符串中,id只会在最后位置



先谢谢。


If my string is like this means,I am not able to retriev id from string "999"


Note: In my String always "id" will be in last position only

Thanks in Advance.

推荐答案

目前尚不清楚两个括号之间是什么意思。你可能不会想到它,但(小)(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);


这篇关于如何使用.Net(windowsapplication)提取位于两个(括号)之间的文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆