我有一些字符串。我想例如第一和最後一個單引號 之間總字符串:正則表達式 - 第一個和最後一個單引號之間的字符串
string val = "'Scale['#13212']'"; //--->Scale['#13212']
string val2= "'Scale[#13212']"; //--->Scale[#13212
string val3="abc'23'asad"; //--->23
我有使用以下[email protected]".*'(.*?)'.*"
,但它僅顯示過去兩年間的字符串。
例如:
string val = "'Scale['#13212']'"; //--->]
當我使用捕捉到的字符串的整個值和一組(在組[1] ONLY)與括一對單引號的它工作正常與貪婪 但是,當我想捕捉一個字符串的全部價值和一組(只有組[1])與多對單引號的封裝,它只能捕捉字符串的值與最後對但不封閉第一個和最後一個單引號之間的字符串。
例如:
string val1 = "Content:abc'23'asad"; //--->23
string val2 = "Content:'Scale['#13212']'ta";
Match match1 = Regex.Match(val1, @".*'(.*)'.*");
Match match2 = Regex.Match(val2, @".*'(.*)'.*");
if (match1.Success)
{
string value1 = match1.Value;
string GroupValue1 = match1.Groups[1].Value;
Console.WriteLine(value1);
Console.WriteLine(GroupValue1);
string value2 = match2.Value;
string GroupValue2 = match2.Groups[1].Value;
Console.WriteLine(value2);
Console.WriteLine(GroupValue2);
Console.ReadLine();
// using greedy For val1 i am getting perfect value for-
// value1--->Content:abc'23'asad
// GroupValue1--->23
// BUT using greedy For val2 i am getting the string elcosed by last single quote-
// value2--->Content:'Scale['#13212']'ta
// GroupValue2---> ]
// But i want GroupValue2--->Scale['#13212']
}
請幫幫忙!
答案在[this linked post](http://stackoverflow.com/a/5662956/3832970)。 –
編輯問題 – one010