Vb.net-regular-expressions

提供:Dev Guides
移動先:案内検索

VB.Net-正規表現

  • 正規表現*は、入力テキストと照合できるパターンです。 .Netフレームワークは、このようなマッチングを可能にする正規表現エンジンを提供します。 パターンは、1つ以上の文字リテラル、演算子、または構造で構成されます。

正規表現を定義するための構成

正規表現を定義できる文字、演算子、および構造のさまざまなカテゴリがあります。 次のリンクをクリックして、これらの構造を見つけます。

  • リンク:/vb.net/vb.net_character_escapes [文字のエスケープ]
  • リンク:/vb.net/vb.net_character_classes [文字クラス]
  • リンク:/vb.net/vb.net_anchors [アンカー]
  • リンク:/vb.net/vb.net_grouping_constructs [グループ化構成]
  • リンク:/vb.net/vb.net_quantifiers [Quantifiers]
  • リンク:/vb.net/vb.net_backreference_constructs [バックリファレンスコンストラクト]
  • リンク:/vb.net/vb.net_alternation_constructs [代替構成]
  • リンク:/vb.net/vb.net_substitutions [Substitutions] *リンク:/vb.net/vb.net_miscellaneous_constructs [その他の構成]

正規表現クラス

Regexクラスは、正規表現を表すために使用されます。

正規表現クラスには、次の一般的に使用されるメソッドがあります-

Sr.No. Methods & Description
1
  • Public Function IsMatch (input As String) As Boolean*

Regexコンストラクターで指定された正規表現が、指定された入力文字列で一致を見つけるかどうかを示します。

2

Public Function IsMatch (input As String, startat As Integer ) As Boolean

Regexコンストラクターで指定された正規表現が、文字列の指定された開始位置から始まる、指定された入力文字列で一致を見つけるかどうかを示します。

3

Public Shared Function IsMatch (input As String, pattern As String ) As Boolean

指定された正規表現が指定された入力文字列で一致を見つけるかどうかを示します。

4

Public Function Matches (input As String) As MatchCollection

正規表現のすべての出現について、指定された入力文字列を検索します。

5

Public Function Replace (input As String, replacement As String) As String

指定された入力文字列で、正規表現パターンに一致するすべての文字列を指定された置換文字列に置き換えます。

6

Public Function Split (input As String) As String()

Regexコンストラクターで指定された正規表現パターンで定義された位置で、入力文字列を部分文字列の配列に分割します。

メソッドとプロパティの完全なリストについては、Microsoftのドキュメントを参照してください。

例1

次の例では、「S」で始まる単語に一致します-

Imports System.Text.RegularExpressions
Module regexProg
   Sub showMatch(ByVal text As String, ByVal expr As String)
      Console.WriteLine("The Expression: " + expr)
      Dim mc As MatchCollection = Regex.Matches(text, expr)
      Dim m As Match

      For Each m In mc
         Console.WriteLine(m)
      Next m
   End Sub

   Sub Main()
      Dim str As String = "A Thousand Splendid Suns"
      Console.WriteLine("Matching words that start with 'S': ")
      showMatch(str, "\bS\S*")
      Console.ReadKey()
   End Sub
End Module

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns

例2

次の例では、「m」で始まり「e」で終わる単語に一致します-

Imports System.Text.RegularExpressions
Module regexProg
   Sub showMatch(ByVal text As String, ByVal expr As String)
      Console.WriteLine("The Expression: " + expr)
      Dim mc As MatchCollection = Regex.Matches(text, expr)
      Dim m As Match

      For Each m In mc
         Console.WriteLine(m)
      Next m
   End Sub

   Sub Main()
      Dim str As String = "make a maze and manage to measure it"
      Console.WriteLine("Matching words that start with 'm' and ends with 'e': ")
      showMatch(str, "\bm\S*e\b")
      Console.ReadKey()
   End Sub
End Module

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure

実施例3

この例では、余分な空白を置き換えます-

Imports System.Text.RegularExpressions
Module regexProg
   Sub Main()
      Dim input As String = "Hello    World   "
      Dim pattern As String = "\\s+"
      Dim replacement As String = " "
      Dim rgx As Regex = New Regex(pattern)
      Dim result As String = rgx.Replace(input, replacement)

      Console.WriteLine("Original String: {0}", input)
      Console.WriteLine("Replacement String: {0}", result)
      Console.ReadKey()
   End Sub
End Module

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Original String: Hello   World
Replacement String: Hello World