■ Regex.IsMatch()
private void button1_Click(object sender, EventArgs e)
{
string tel = "090-XXXX-YYYY";
bool isMatch = Regex.IsMatch(tel, "090");
if (isMatch)
{
this.label1.Text = "Found it!";
}
}
private void button2_Click(object sender, EventArgs e)
{
string tel = "090-XXXX-YYYY";
var match = Regex.Match(tel, "090");
this.label1.Text = string.Format("Index={0}, Length={1}, Value={2}",
match.Index, match.Length, match.Value);
}
出力結果
Index=0, Length=3, Value=090
■ Regex.NextMatch()
private void button3_Click(object sender, EventArgs e)
{
string tel = "090-XXXX-Y090";
var match = Regex.Match(tel, "090");
string str = string.Empty;
while (match.Success)
{
str += string.Format(
"Index={0} Length={1} Value={2} {3}",
match.Index, match.Length, match.Value, "/ ");
match = match.NextMatch();
}
this.label1.Text = str;
}
■ 別方法:Regex.Matches()
private void button4_Click(object sender, EventArgs e)
{
string tel = "090-XXXX-Y090";
var matches = Regex.Matches(tel, "090");
string str = string.Empty;
foreach (Match match in matches)
{
str += string.Format(
"Index={0} Length={1} Value={2} {3}",
match.Index, match.Length, match.Value, "/ ");
}
this.label1.Text = str;
}
出力結果
Index=0, Length=3, Value=090 / Index=10, Length=3, Value=090 /