Vbscript-reg-expressions
VBScript-正規表現
正規表現は、パターンを形成する一連の文字であり、主に検索と置換に使用されます。 パターンを作成する目的は、特定の文字列を照合することで、開発者は条件に基づいて文字を抽出し、特定の文字を置き換えることができます。
RegExpオブジェクト
RegExpオブジェクトは、開発者が文字列のパターンを一致させるのに役立ち、プロパティとメソッドは正規表現を簡単に操作するのに役立ちます。 JavaScriptのRegExpに似ています
プロパティ
- Pattern -Patternメソッドは、正規表現を定義するために使用される文字列を表し、正規表現オブジェクトを使用する前に設定する必要があります。
- IgnoreCase -trueまたはfalseの場合、文字列内のすべての可能な一致に対して正規表現をテストする必要があるかどうかを表すブールプロパティ。 明示的に指定されていない場合、IgnoreCase値はFalseに設定されます。
- Global -文字列内のすべての可能な一致に対して正規表現をテストする必要があるかどうかを表すブールプロパティ。 明示的に指定されていない場合、グローバル値はFalseに設定されます。
方法
- Test (search-string)-Testメソッドは引数として文字列を受け取り、正規表現が文字列と正常に一致する場合はTrueを返し、そうでない場合はFalseを返します。
- Replace (search-string、replace-string)-Replaceメソッドは2つのパラメーターを取ります。 検索が成功すると、その一致をreplace-stringに置き換え、新しい文字列が返されます。 一致するものがない場合、元の検索文字列が返されます。
- Execute (search-string)-ExecuteメソッドはReplaceと同様に機能しますが、成功した各マッチのMatchオブジェクトを含むMatchesコレクションオブジェクトを返す点が異なります。 元の文字列は変更されません。
コレクションオブジェクトに一致
Matchesコレクションオブジェクトは、Executeメソッドの結果として返されます。 このコレクションオブジェクトには、0個以上のMatchオブジェクトを含めることができ、このオブジェクトのプロパティは読み取り専用です。
- Count -Countメソッドは、コレクション内の一致オブジェクトの数を表します。
- Item -Itemメソッドを使用すると、matchesコレクションオブジェクトからmatchオブジェクトにアクセスできます。
一致オブジェクト
Matchオブジェクトは、matchesコレクションオブジェクト内に含まれています。 これらのオブジェクトは、文字列の検索後の一致の成功を表します。
- FirstIndex -一致が発生した元の文字列内の位置を表します。 このインデックスはゼロベースです。つまり、文字列の最初の位置は0です。
- 長さ-一致した文字列の全長を表す値。
- 値-一致した値またはテキストを表す値。 また、Matchオブジェクトにアクセスするときのデフォルト値です。
パターンパラメーターについて
パターンの構築はPERLに似ています。 正規表現を使用する場合、パターンの構築が最も重要です。 このセクションでは、さまざまな要因に基づいてパターンを作成する方法を扱います。
位置合わせ
位置一致の重要性は、正規表現を正しい場所に配置することです。
| Symbol | Description |
|---|---|
| ^ | Matches only the beginning of a string. |
| $ | Match only the end of a string. |
| \b | Matches any word boundary |
| \B | Matches any non-word boundary |
リテラルマッチング
アルファベット、数字、特殊文字、または10進数、16進数などの任意の形式の文字をリテラルとして扱うことができます。 正規表現のコンテキスト内ですでに特別な意味を持つ文字はほとんどないため、エスケープシーケンスを使用してエスケープする必要があります。
| Symbol | Description |
|---|---|
| Alphanumeric | Matches alphabetical and numerical characters only. |
| \n | Matches a new line. |
| \[ | Matches [ literal only |
| \] | Matches ] literal only |
| \( | Matches ( literal only |
| \) | Matches ) literal only |
| \t | Matches horizontal tab |
| \v | Matches vertical tab |
| Matches | |
| literal only | \\{ |
| Matches \{ literal only | \} |
| Matches } literal only | \\ |
| Matches \ literal only | \? |
| Matches ? literal only | \ * |
| Matches* literal only | \+ |
| Matches + literal only | \. |
| Matches . literal only | \b |
| Matches any word boundary | \B |
| Matches any non-word boundary | \f |
| Matches a form feed | \r |
| Matches carriage return | \xxx |
| Matches the ASCII character of an octal number xxx. | \xdd |
| Matches the ASCII character of an hexadecimal number dd. | \uxxxx |
一致する文字クラス
文字クラスは、カスタマイズされたグループ化によって形成され、[]括弧で囲まれたパターンです。 リストに含まれてはならない文字クラスを想定している場合は、キャップ^である負の記号を使用して特定の文字クラスを無視する必要があります。
| Symbol | Description |
|---|---|
| [xyz] | Match any of the character class enclosed within the character set. |
| [^xyz] | Matches any of the character class that are NOT enclosed within the character set. |
| . | Matches any character class except \n |
| \w | Match any word character class. Equivalent to [a-zA-Z_0-9] |
| \W | Match any non-word character class. Equivalent to [^a-zA-Z_0-9] |
| \d | Match any digit class. Equivalent to [0-9]. |
| \D | Match any non-digit character class. Equivalent to [^0-9]. |
| \s | Match any space character class. Equivalent to [ \t\r\n\v\f] |
| \S | Match any space character class. Equivalent to [^\t\r\n\v\f] |
繰り返しマッチング
繰り返し一致により、正規表現内で複数の検索が可能になります。 また、正規表現で要素が繰り返される回数も指定します。
| Symbol | Description |
|---|---|
| * | Matches zero or more occurrences of the given regular Expression. Equivalent to \{0,}. |
| + | Matches one or more occurrences of the given regular Expression. Equivalent to \{1,}. |
| ? | Matches zero or one occurrences of the given regular Expression. Equivalent to \{0,1}. |
| {x} | Matches exactly x number of occurrences of the given regular expression. |
| \{x,} | Match atleast x or more occurrences of the given regular expression. |
| \{x,y} | Matches x to y number of occurences of the given regular expression. |
交替とグループ化
交替とグループ化により、開発者は、正規表現内の複雑な句を処理する際に、より複雑な正規表現を作成でき、柔軟性と制御性が向上します。
| Symbol | Description |
|---|---|
| 0 | Grouping a clause to create a clause. "(xy)?(z)" matches "xyz" or "z". |
| Alternation combines one regular expression clause and then matches any of the individual clauses. "(ij) | (23) |
正規表現の構築
以下に、正規表現の作成方法を明確に説明するいくつかの例を示します。
| Regular Expression | Description |
|---|---|
| "^\s*.." and "..\s*$" | Represents that there can be any number of leading and trailing space characters in a single line. |
| "((\$\s?) | (#\s?))?" |
| Represents an optional $ or # sign followed by an optional space. | "\d+(\.(\d\d)?)?\d+(\.(\d\d)?)?" |
例
以下の例は、ユーザーが、@が続き、その後にドメイン名が続くメールIDが存在するような形式に一致するメールIDを入力したかどうかをチェックします。
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
strid = "[email protected]"
Set re = New RegExp
With re
.Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
.IgnoreCase = False
.Global = False
End With
' Test method returns TRUE if a match is found
If re.Test( strid ) Then
Document.write(strid & " is a valid e-mail address")
Else
Document.write(strid & " is NOT a valid e-mail address")
End If
Set re = Nothing
</script>
</body>
</html>