I'd like to capture the value of the Initial Catalog in this string:
我想在此字符串中捕获初始目录的值:
"blah blah Initial Catalog = MyCat'"
“blah blah初始目录= MyCat'”
I'd like the result to be: MyCat
我希望结果是:MyCat
There could or could not be spaces before and after the equal sign and there could or could not be spaces before the single quote.
在等号之前和之后可能存在或不存在空格,并且在单引号之前可能存在或不存在空格。
Tried this and various others but no go:
试过这个和其他各种但是没有去:
/Initial Catalog\s?=\s?.*\s?\'/
Using .Net.
使用.Net。
3 个解决方案
#1
1
This is a nice regex
这是一个很好的正则表达式
= *(.*?) *'
Use the idea and add \s and more literal text as needed.
使用该想法并根据需要添加\ s和更多文字文本。
In C# group 1 will contain the match
在C#中,第1组将包含匹配项
string resultString = null;
try {
Regex regexObj = new Regex("= *(.*?) *'");
resultString = regexObj.Match(subjectString).Groups[1].Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
#2
2
You need to put parentheses around the part of the string that you would like to match:
您需要在要匹配的字符串部分周围加上括号:
/Initial Catalog\s*=\s*(.*?)\s*'/
Also you would like to exclude as many spaces as possible before the '
, so you need \s*
rather than \s?
. The .*?
means that the extracted part of the string doesn't take those spaces, since it is now lazy.
你也想在'之前排除尽可能多的空格,所以你需要\ s *而不是\ s?。 。*?意味着字符串的提取部分不占用那些空格,因为它现在是懒惰的。
#3
0
Regex rgx = new Regex(@"=\s*([A-z]+)\s*'");
String result = rgx.Match(text).Groups[1].Value;
#1
1
This is a nice regex
这是一个很好的正则表达式
= *(.*?) *'
Use the idea and add \s and more literal text as needed.
使用该想法并根据需要添加\ s和更多文字文本。
In C# group 1 will contain the match
在C#中,第1组将包含匹配项
string resultString = null;
try {
Regex regexObj = new Regex("= *(.*?) *'");
resultString = regexObj.Match(subjectString).Groups[1].Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
#2
2
You need to put parentheses around the part of the string that you would like to match:
您需要在要匹配的字符串部分周围加上括号:
/Initial Catalog\s*=\s*(.*?)\s*'/
Also you would like to exclude as many spaces as possible before the '
, so you need \s*
rather than \s?
. The .*?
means that the extracted part of the string doesn't take those spaces, since it is now lazy.
你也想在'之前排除尽可能多的空格,所以你需要\ s *而不是\ s?。 。*?意味着字符串的提取部分不占用那些空格,因为它现在是懒惰的。
#3
0
Regex rgx = new Regex(@"=\s*([A-z]+)\s*'");
String result = rgx.Match(text).Groups[1].Value;