using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"gr[ae]y\s\S+?[\s\p{P}]";
string input = "The gray wolf jumped over the grey wall.";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
Console.WriteLine($"'{match.Value}'");
}
}
// The example displays the following output:// 'gray wolf '// 'grey wall.'
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "gr[ae]y\s\S+?[\s\p{P}]"
Dim input As String = "The gray wolf jumped over the grey wall."
Dim matches As MatchCollection = Regex.Matches(input, pattern)
For Each match As Match In matches
Console.WriteLine($"'{match.Value}'")
Next
End Sub
End Module
' The example displays the following output:
' 'gray wolf '
' 'grey wall.'
正規表現パターン gr[ae]y\s\S+?[\s|\p{P}] は、次のように定義されます。
Pattern
説明
gr
リテラル文字 "gr" と一致します。
[ae]
"a" または "e" と一致します。
y\s
リテラル文字 "y" の後に空白文字が続く語と一致します。
\S+?
1 つ以上 (ただし、できるだけ少ない数) の空白以外の文字と一致します。
[\s\p{P}]
空白文字または句読点と一致します。
次の例は、大文字で始まる語と一致します。 部分式 [A-Z] を使用して、A から Z の範囲の大文字を表します。
C#
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\b[A-Z]\w*\b";
string input = "A city Albany Zulu maritime Marseilles";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Value);
}
}
// The example displays the following output:// A// Albany// Zulu// Marseilles
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b[A-Z]\w*\b"
Dim input As String = "A city Albany Zulu maritime Marseilles"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Value)
Next
End Sub
End Module
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\bth[^o]\w+\b";
string input = "thought thing though them through thus thorough this";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Value);
}
}
// The example displays the following output:// thing// them// through// thus// this
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\bth[^o]\w+\b"
Dim input As String = "thought thing though them through thus " + _
"thorough this"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Value)
Next
End Sub
End Module
' The example displays the following output:
' thing
' them
' through
' thus
' this
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = "^.+";
string input = "This is one line and" + Environment.NewLine + "this is the second.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(Regex.Escape(match.Value));
Console.WriteLine();
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Singleline))
Console.WriteLine(Regex.Escape(match.Value));
}
}
// The example displays the following output:// This\ is\ one\ line\ and\r//// This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "^.+"
Dim input As String = "This is one line and" + vbCrLf + "this is the second."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(Regex.Escape(match.Value))
Next
Console.WriteLine()
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.SingleLine)
Console.WriteLine(Regex.Escape(match.Value))
Next
End Sub
End Module
' The example displays the following output:
' This\ is\ one\ line\ and\r
'
' This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\b.*[.?!;:](\s|\z)";
string input = "this. what: is? go, thing.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Value);
}
}
// The example displays the following output:// this. what: is? go, thing.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As STring = "\b.*[.?!;:](\s|\z)"
Dim input As String = "this. what: is? go, thing."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Value)
Next
End Sub
End Module
' The example displays the following output:
' this. what: is? go, thing.
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\b(\p{IsGreek}+(\s)?)+\p{Pd}\s(\p{IsBasicLatin}+(\s)?)+";
string input = "Κατα Μαθθαίον - The Gospel of Matthew";
Console.WriteLine(Regex.IsMatch(input, pattern)); // Displays True.
}
}
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b(\p{IsGreek}+(\s)?)+\p{Pd}\s(\p{IsBasicLatin}+(\s)?)+"
Dim input As String = "Κατα Μαθθαίον - The Gospel of Matthew"
Console.WriteLine(Regex.IsMatch(input, pattern)) ' Displays True.
End Sub
End Module
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"(\P{Sc})+";
string[] values = { "$164,091.78", "£1,073,142.68", "73¢", "€120" };
foreach (stringvaluein values)
Console.WriteLine(Regex.Match(value, pattern).Value);
}
}
// The example displays the following output:// 164,091.78// 1,073,142.68// 73// 120
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "(\P{Sc})+"
Dim values() As String = {"$164,091.78", "£1,073,142.68", "73¢", "€120"}
For Each value As String In values
Console.WriteLine(Regex.Match(value, pattern).Value)
Next
End Sub
End Module
' The example displays the following output:
' 164,091.78
' 1,073,142.68
' 73
' 120
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"(\w)\1";
string[] words = { "trellis", "seer", "latter", "summer",
"hoarse", "lesser", "aardvark", "stunned" };
foreach (string word in words)
{
Match match = Regex.Match(word, pattern);
if (match.Success)
Console.WriteLine($"'{match.Value}' found in '{word}' at position {match.Index}.");
else
Console.WriteLine($"No double characters in '{word}'.");
}
}
}
// The example displays the following output:// 'll' found in 'trellis' at position 3.// 'ee' found in 'seer' at position 1.// 'tt' found in 'latter' at position 2.// 'mm' found in 'summer' at position 2.// No double characters in 'hoarse'.// 'ss' found in 'lesser' at position 2.// 'aa' found in 'aardvark' at position 0.// 'nn' found in 'stunned' at position 3.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "(\w)\1"
Dim words() As String = {"trellis", "seer", "latter", "summer", _
"hoarse", "lesser", "aardvark", "stunned"}
For Each word As String In words
Dim match As Match = Regex.Match(word, pattern)
If match.Success Then
Console.WriteLine("'{0}' found in '{1}' at position {2}.", _
match.Value, word, match.Index)
Else
Console.WriteLine("No double characters in '{0}'.", word)
End If
Next
End Sub
End Module
' The example displays the following output:
' 'll' found in 'trellis' at position 3.
' 'ee' found in 'seer' at position 1.
' 'tt' found in 'latter' at position 2.
' 'mm' found in 'summer' at position 2.
' No double characters in 'hoarse'.
' 'ss' found in 'lesser' at position 2.
' 'aa' found in 'aardvark' at position 0.
' 'nn' found in 'stunned' at position 3.
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\b(\w+)(\W){1,2}";
string input = "The old, grey mare slowly walked across the narrow, green pasture.";
foreach (Match match in Regex.Matches(input, pattern))
{
Console.WriteLine(match.Value);
Console.Write(" Non-word character(s):");
CaptureCollection captures = match.Groups[2].Captures;
for (int ctr = 0; ctr < captures.Count; ctr++)
Console.Write(@"'{0}' (\u{1}){2}", captures[ctr].Value,
Convert.ToUInt16(captures[ctr].Value[0]).ToString("X4"),
ctr < captures.Count - 1 ? ", " : "");
Console.WriteLine();
}
}
}
// The example displays the following output:// The// Non-word character(s):' ' (\u0020)// old,// Non-word character(s):',' (\u002C), ' ' (\u0020)// grey// Non-word character(s):' ' (\u0020)// mare// Non-word character(s):' ' (\u0020)// slowly// Non-word character(s):' ' (\u0020)// walked// Non-word character(s):' ' (\u0020)// across// Non-word character(s):' ' (\u0020)// the// Non-word character(s):' ' (\u0020)// narrow,// Non-word character(s):',' (\u002C), ' ' (\u0020)// green// Non-word character(s):' ' (\u0020)// pasture.// Non-word character(s):'.' (\u002E)
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b(\w+)(\W){1,2}"
Dim input As String = "The old, grey mare slowly walked across the narrow, green pasture."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Value)
Console.Write(" Non-word character(s):")
Dim captures As CaptureCollection = match.Groups(2).Captures
For ctr As Integer = 0 To captures.Count - 1
Console.Write("'{0}' (\u{1}){2}", captures(ctr).Value, _
Convert.ToUInt16(captures(ctr).Value.Chars(0)).ToString("X4"), _
If(ctr < captures.Count - 1, ", ", ""))
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' The
' Non-word character(s):' ' (\u0020)
' old,
' Non-word character(s):',' (\u002C), ' ' (\u0020)
' grey
' Non-word character(s):' ' (\u0020)
' mare
' Non-word character(s):' ' (\u0020)
' slowly
' Non-word character(s):' ' (\u0020)
' walked
' Non-word character(s):' ' (\u0020)
' across
' Non-word character(s):' ' (\u0020)
' the
' Non-word character(s):' ' (\u0020)
' narrow,
' Non-word character(s):',' (\u002C), ' ' (\u0020)
' green
' Non-word character(s):' ' (\u0020)
' pasture.
' Non-word character(s):'.' (\u002E)
2 番目のキャプチャ グループの Group オブジェクトには、キャプチャされた単語に使用されない文字が 1 つだけ含まれるので、この例では、CaptureCollection プロパティによって返される Group.Captures オブジェクトから、キャプチャされたすべての単語に使用されない文字を取得します。
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\b\w+(e)?s(\s|$)";
string input = "matches stores stops leave leaves";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Value);
}
}
// The example displays the following output:// matches// stores// stops// leaves
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b\w+(e)?s(\s|$)"
Dim input As String = "matches stores stops leave leaves"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Value)
Next
End Sub
End Module
' The example displays the following output:
' matches
' stores
' stops
' leaves
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"\b(\S+)\s?";
string input = "This is the first sentence of the first paragraph. " +
"This is the second sentence.\n" +
"This is the only sentence of the second paragraph.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Groups[1]);
}
}
// The example displays the following output:// This// is// the// first// sentence// of// the// first// paragraph.// This// is// the// second// sentence.// This// is// the// only// sentence// of// the// second// paragraph.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b(\S+)\s?"
Dim input As String = "This is the first sentence of the first paragraph. " + _
"This is the second sentence." + vbCrLf + _
"This is the only sentence of the second paragraph."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Groups(1))
Next
End Sub
End Module
' The example displays the following output:
' This
' is
' the
' first
' sentence
' of
' the
' first
' paragraph.
' This
' is
' the
' second
' sentence.
' This
' is
' the
' only
' sentence
' of
' the
' second
' paragraph.
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"^(\(?\d{3}\)?[\s-])?\d{3}-\d{4}$";
string[] inputs = { "111 111-1111", "222-2222", "222 333-444",
"(212) 111-1111", "111-AB1-1111",
"212-111-1111", "01 999-9999" };
foreach (string input in inputs)
{
if (Regex.IsMatch(input, pattern))
Console.WriteLine(input + ": matched");
else
Console.WriteLine(input + ": match failed");
}
}
}
// The example displays the following output:// 111 111-1111: matched// 222-2222: matched// 222 333-444: match failed// (212) 111-1111: matched// 111-AB1-1111: match failed// 212-111-1111: matched// 01 999-9999: match failed
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "^(\(?\d{3}\)?[\s-])?\d{3}-\d{4}$"
Dim inputs() As String = {"111 111-1111", "222-2222", "222 333-444", _
"(212) 111-1111", "111-AB1-1111", _
"212-111-1111", "01 999-9999"}
For Each input As String In inputs
If Regex.IsMatch(input, pattern) Then
Console.WriteLine(input + ": matched")
Else
Console.WriteLine(input + ": match failed")
End If
Next
End Sub
End Module
' The example displays the following output:
' 111 111-1111: matched
' 222-2222: matched
' 222 333-444: match failed
' (212) 111-1111: matched
' 111-AB1-1111: match failed
' 212-111-1111: matched
' 01 999-9999: match failed
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string pattern = @"^\D\d{1,5}\D*$";
string[] inputs = { "A1039C", "AA0001", "C18A", "Y938518" };
foreach (string input in inputs)
{
if (Regex.IsMatch(input, pattern))
Console.WriteLine(input + ": matched");
else
Console.WriteLine(input + ": match failed");
}
}
}
// The example displays the following output:// A1039C: matched// AA0001: match failed// C18A: matched// Y938518: match failed
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "^\D\d{1,5}\D*$"
Dim inputs() As String = {"A1039C", "AA0001", "C18A", "Y938518"}
For Each input As String In inputs
If Regex.IsMatch(input, pattern) Then
Console.WriteLine(input + ": matched")
Else
Console.WriteLine(input + ": match failed")
End If
Next
End Sub
End Module
' The example displays the following output:
サポートされている Unicode 一般カテゴリ
Unicode は、次の表に示されている一般カテゴリを定義しています。 詳細については、「Unicode Character Database (Unicode 文字データベース)」内の「UCD File Format (UCD ファイル形式)」および「General Category Values (一般カテゴリの値)」の各サブトピックを参照してください (セクション 5.7.1、表 12)。
カテゴリ
説明
Lu
Letter, Uppercase (字、大文字)
Ll
Letter, Lowercase (字、小文字)
Lt
Letter, Titlecase (字、タイトル文字)
Lm
Letter, Modifier (字、修飾)
Lo
Letter, Other (字、その他)
L
すべてのアルファベット文字。 これには、Lu、Ll、Lt、Lm、および Lo の各文字が含まれます。
Mn
Mark, Nonspacing (結合文字、幅なし)
Mc
Mark, Spacing Combining (結合文字、幅あり)
Me
Mark, Enclosing (結合文字、囲み)
M
すべての結合マーク。 これには、Mn、Mc、および Me の各カテゴリが含まれます。
Nd
Number, Decimal Digit (数、10 進数字)
Nl
Number, Letter (数、字)
No
Number, Other (数、その他)
N
すべての数。 これには、Nd、Nl、および No の各カテゴリが含まれます。
Pc
Punctuation, Connector (句読点、接続)
Pd
Punctuation, Dash (句読点、ダッシュ)
Ps
Punctuation, Open (句読点、開き)
Pe
Punctuation, Close (句読点、閉じ)
Pi
Punctuation, Initial quote (句読点、開始引用符。使用状況に応じて Ps または Pe のように動作)
Pf
Punctuation, Final quote (句読点、終了引用符。使用状況に応じて Ps または Pe のように動作)
Po
Punctuation, Other (句読点、その他)
P
すべての句読点。 これには、Pc、Pd、Ps、Pe、Pi、Pf、および Po の各カテゴリが含まれます。
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
char[] chars = { 'a', 'X', '8', ',', ' ', '\u0009', '!' };
foreach (char ch in chars)
Console.WriteLine($"'{Regex.Escape(ch.ToString())}': {Char.GetUnicodeCategory(ch)}");
}
}
// The example displays the following output:// 'a': LowercaseLetter// 'X': UppercaseLetter// '8': DecimalDigitNumber// ',': OtherPunctuation// '\ ': SpaceSeparator// '\t': Control// '!': OtherPunctuation
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim chars() As Char = {"a"c, "X"c, "8"c, ","c, " "c, ChrW(9), "!"c}
For Each ch As Char In chars
Console.WriteLine("'{0}': {1}", Regex.Escape(ch.ToString()), _
Char.GetUnicodeCategory(ch))
Next
End Sub
End Module
' The example displays the following output:
' 'a': LowercaseLetter
' 'X': UppercaseLetter
' '8': DecimalDigitNumber
' ',': OtherPunctuation
' '\ ': SpaceSeparator
' '\t': Control
' '!': OtherPunctuation
using System;
using System.Text.RegularExpressions;
publicclassExample
{
publicstaticvoidMain()
{
string[] inputs = { "123", "13579753", "3557798", "335599901" };
string pattern = @"^[0-9-[2468]]+$";
foreach (string input in inputs)
{
Match match = Regex.Match(input, pattern);
if (match.Success)
Console.WriteLine(match.Value);
}
}
}
// The example displays the following output:// 13579753// 335599901
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim inputs() As String = {"123", "13579753", "3557798", "335599901"}
Dim pattern As String = "^[0-9-[2468]]+$"
For Each input As String In inputs
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then Console.WriteLine(match.Value)
Next
End Sub
End Module
' The example displays the following output:
' 13579753
' 335599901