问题描述
- C#正则表达式如何取出某字符串等于号后的括号前的数据
- (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ahnu)(PORT = 1521)))(CONNECT_DATA = (SID =orcl)))如何取出host等于什么,Port等于什么,sid等于什么
解决方案
<?<=[A-Z]+s=>[A-Z]+
解决方案二:
PROTOCOL=.*)?
PORT=.*)?
解决方案三:
这样写为什么不对
``` string str = ""(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ahnu)(PORT = 1521)))(CONNECT_DATA = (SID =orcl)))"";
str = str.Trim();
str=str.ToUpper();
Regex reg = new Regex(@""^(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=(w+))(PORT=(w+))))(CONNECT_DATA=(SID=(w+))))$"");
Match m = reg.Match(str);
if (m.Success)
解决方案四:
搞定了
``` string str = ""(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ahnu)(PORT = 1521)))(CONNECT_DATA = (SID =orcl)))"";
str = str.Trim();
Match m = Regex.Match(str @"".+?=.+?=.+?=(.+?))).+?=(.+?)))"");
if (m.Success)
{
string s1 = m.Groups[1].Value;//"" (PROTOCOL = TCP)(HOST = AHNU)(PORT = 1521""
string s2 = m.Groups[2].Value;// (SID =ORCL""
if (m.Success)
{
string str3 = m.Groups[1].Value;
string str4 = m.Groups[2].Value; //"" (PROTOCOL = TCP)(HOST = AHNU)(PORT = 1521""
m = Regex.Match(s1 @""(.+?=.+?)(.+?=(.+?))(.+?=(.+)"");
if (m.Success)
{
string host = m.Groups[1].Value;
string port = m.Groups[2].Value;
}
m = Regex.Match(s2 @""(.+?=(.+)"");
if (m.Success)
{
string SID = m.Groups[1].Value;
}
}
解决方案五:
搞定了
``` string str = ""(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ahnu)(PORT = 1521)))(CONNECT_DATA = (SID =orcl)))"";
str = str.Trim();
Match m = Regex.Match(str @"".+?=.+?=.+?=(.+?))).+?=(.+?)))"");
if (m.Success)
{
string s1 = m.Groups[1].Value;//"" (PROTOCOL = TCP)(HOST = AHNU)(PORT = 1521""
string s2 = m.Groups[2].Value;// (SID =ORCL""
if (m.Success)
{
string str3 = m.Groups[1].Value;
string str4 = m.Groups[2].Value; //"" (PROTOCOL = TCP)(HOST = AHNU)(PORT = 1521""
m = Regex.Match(s1 @""(.+?=.+?)(.+?=(.+?))(.+?=(.+)"");
if (m.Success)
{
string host = m.Groups[1].Value;
string port = m.Groups[2].Value;
}
m = Regex.Match(s2 @""(.+?=(.+)"");
if (m.Success)
{
string SID = m.Groups[1].Value;
}
}
解决方案六:
搞定了
``` string str = ""(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ahnu)(PORT = 1521)))(CONNECT_DATA = (SID =orcl)))"";
str = str.Trim();
Match m = Regex.Match(str @"".+?=.+?=.+?=(.+?))).+?=(.+?)))"");
if (m.Success)
{
string s1 = m.Groups[1].Value;//"" (PROTOCOL = TCP)(HOST = AHNU)(PORT = 1521""
string s2 = m.Groups[2].Value;// (SID =ORCL""
if (m.Success)
{
string str3 = m.Groups[1].Value;
string str4 = m.Groups[2].Value; //"" (PROTOCOL = TCP)(HOST = AHNU)(PORT = 1521""
m = Regex.Match(s1 @""(.+?=.+?)(.+?=(.+?))(.+?=(.+)"");
if (m.Success)
{
string host = m.Groups[1].Value;
string port = m.Groups[2].Value;
}
m = Regex.Match(s2 @""(.+?=(.+)"");
if (m.Success)
{
string SID = m.Groups[1].Value;
}
}