我是RegEx的新手。我有一個像下面的字符串。我想[{##}]C#RegEx查找字符串中的值
防爆之間的值:"Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]"
我想從上面的字符串得到以下值。基於該解決方案
"John",
"ABC Bank",
"Houston"
我是RegEx的新手。我有一個像下面的字符串。我想[{##}]C#RegEx查找字符串中的值
防爆之間的值:"Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]"
我想從上面的字符串得到以下值。基於該解決方案
"John",
"ABC Bank",
"Houston"
基於解決方案Regular Expression Groups in C#。 你可以試試這個:
string sentence = "Employee name is [{#john#}], works for [{#ABC BANK#}],
[{#Houston#}]";
string pattern = @"\[\{\#(.*?)\#\}\]";
foreach (Match match in Regex.Matches(sentence, pattern))
{
if (match.Success && match.Groups.Count > 0)
{
var text = match.Groups[1].Value;
Console.WriteLine(text);
}
}
Console.ReadLine();
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Test("the quick brown [{#fox#}] jumps over the lazy dog."));
Console.ReadLine();
}
public static string Test(string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
var result = System.Text.RegularExpressions.Regex.Replace(str, @".*\[{#", string.Empty, RegexOptions.Singleline);
result = System.Text.RegularExpressions.Regex.Replace(result, @"\#}].*", string.Empty, RegexOptions.Singleline);
return result;
}
}
}
和awesome breakdown for matching patterns inside wrapping patterns你可以嘗試:
\[\{\#(?<Text>(?:(?!\#\}\]).)*)\#\}\]
凡\[\{\#
是[{#
和\#\}\]
你逃脫片頭是#}]
逃脫關閉順序。
你的內在價值是一個名爲Text
匹配的小組。
string strRegex = @"\[\{\#(?<Text>(?:(?!\#\}\]).)*)\#\}\]";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);
string strTargetString = @"Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
var text = myMatch.Groups["Text"].Value;
// TODO: Do something with it.
}
}
你應該看看[問] – pvg
什麼是「#」?數字?什麼? – anomeric
這不是最好的作品不斷,但他希望在字符串中提取的散列值之間,看起來像它看起來很清楚對我說:「[{#EXTRACT_THIS#}]」正則表達式可能是這樣做 – Tico