←
▼
▲
Function RegExp::Execute( s as string ) as Matches
正規表現にマッチするかどうかテストし、マッチしたものを返します。
【引数】
s
テストされる文字列
返り値
マッチしたもの
【補足】
に検索条件を設定してから呼び出してください。
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "a.b"
re.Global = True
Set matches = re.Execute( "raxbmacb" )
WScript.echo "Match count = " & matches.Count
For Each match In matches
WScript.echo "FirstIndex = " & match.FirstIndex & _
", Length = " & match.Length & ", Value = " & match.Value
Next
出力
Match count = 2
FirstIndex = 1, Length = 3, Value = axb
FirstIndex = 5, Length = 3, Value = acb
正規表現 = "a.b", テストされる文字列 = "raxbmacb", 複数検索する
サンプル
RegExp::Global = False のときは、Matches::Count は 0 か 1 のどちらかになります。
参考
→ GetLineCount (vbslib)
←
▼
▲
Function RegExp::Test( s as string ) as Boolean
正規表現にマッチするかどうかテストし、1つでもマッチしたかどうかを返します。
【引数】
s
テストされる文字列
【補足】
返り値
True=マッチした文字列があった
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^aaa.*bbb.c"
WScript.echo re.Test("aaaxxxbbbyc")
-1
出力
(True)
サンプル
に検索条件を設定してから呼び出してください。
(もし 0 なら False)
←
▼
▲
Function RegExp::Replace( s as string, to as string ) as string
正規表現を使って、文字列の一部を置換します。
【引数】
s
to
入力文字列(置換前)
置換後の文字列
返り値
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "a.b"
re.Global = True
WScript.echo re.Replace("raxbmacb", "---")
r---m---
出力
出力文字列(置換後)
【補足】
に検索条件を設定してから呼び出してください。
サンプル
サンプル
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "%([0-9]+)"
re.Global = True
WScript.echo re.Replace( "%123 %456", "<$1>" )
出力
<123> <456>
注意: $1 は ${1} ではありません
→ 正規表現を使った置換
参考
←
▼
▲
Dim RegExp::MultiLine as boolean
Pattern に、行頭 "^"、行末 "$" が使えるようにします。
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "A.*"+vbCR+"?"+vbLF+"^D"
re.MultiLine = True
Set matches = re.Execute( "ABC"+ vbCRLF +"DEF"+ vbCRLF )
echo "Match count = " & matches.Count
For Each match In matches
echo "FirstIndex = " & match.FirstIndex & _
", Length = " & match.Length & ", Value = " & match.Value
Next
サンプル
"ABC"+ vbCRLF +"DEF"+ vbCRLF に対して、"A.*"+vbCR+"?"+vbLF+"^D" がマッチするには、
MultiLine を True にする必要があります。 "^" がない "A.*"+vbCR+"?"+vbLF+"D" にマッチ
するには、MultiLine は False でも構いません。
改行文字は、任意の文字 "." や、行頭 "^" や、行末"$" にマッチしないので、 vbCR+"?"+vbLF
を指定する必要があります。 これは、MultiLine の値がどちらでも、違いはありません。
Match count = 1
FirstIndex = 0, Length = 6, Value = ABC
D
出力
←
▼
▲
Integer .FirstIndex
マッチした位置、最初=0、2バイト文字も 1 でカウント
Integer .Length
マッチした文字列の文字数、2バイト文字も 1 でカウント
String .Value
マッチした文字列
サンプル
でテストした結果です。
サンプル
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "<DIV .*>"
re.Global = True
Set matches = re.Execute( text )
For Each match In matches
attr = sscanf( match.Value, "id=""%s""" )
text = Left( text, match.FirstIndex ) +_
"<DIV id="""+ attr +""" plus=""add"">" +_
Mid( text, match.FirstIndex + match.Length + 1 )
Next
match
任意の id属性(ワイルドカード)を持つ DIVタグに、plus 属性を加える
Matches オブジェクトのプロパティ
Match ( Index )
Integer .Count
マッチした数
Index + 1 番目のマッチした内容
←
▼
▲
c = a Mod b
剰余。 あまり。 9 Mod 4 = 1, -9 Mod 4 = -1
16進数の 1F (10進数の値は 31)
&h8000 〜 &hFFFF と、&h80000000 〜 &hFFFFFFFF は、マイナス
-32767〜+32767 は、Integer です。 -32768 は Long です。
Const Err_TestSkip = 2
定数。 参照する場所より上に記述する必要がある
Execute されるコードには再度記述が必要
→ クラスの定数
c = a And b
c = a Or b
論理積
論理和、1 にするビット演算
c = a \ b
商(割った結果を整数にする)
If は、0以外で真になります
hex_str = Hex( 16 )
CInt(1.9) = 2, Int(1.9) = 1, Fix(1.9) = 1
CInt(1.9) = -2, Int(-1.9) = -2, Fix(-1.9) = -1
c = a Xor b
c = a And Not b
排他的論理和
0 にするビット演算
キーワード:
関連
参考
参考
16進数 &h8000 〜 &hFFFF の正の値を取得するとき。
… VBScript では下記 CLng
→ CInt2
関連
→ IsBitSet
\
←
▼
▲
切り下げ (マイナスのとき、小数があると大きい値へ。 C99互換)
切り下げ (マイナスのとき、小数があると小さい値へ)
Int(1.9) = 1
Int(-1.9) = -2
→ 負の割り算、余り、% 演算子
Fix(1.9) = 1
Fix(-1.9) = -1
四捨五入(偶数丸め)
→ 偶数丸め、銀行式丸めの四捨五入
四捨五入(R丸め、0.5以上なら切り上げ)
Int( 1.4 + 0.5 ) = 1
Int( 1.5 + 0.5 ) = 2
Int( 1.6 + 0.5 ) = 2
Int( 2.4 + 0.5 ) = 2
Int( 2.5 + 0.5 ) = 3
Int( 2.6 + 0.5 ) = 3
Fix 関数を使う。
Int 関数を使う。
CInt( 1.4 ) = 1
CInt( 1.5 ) = 2
CInt( 1.6 ) = 2
CInt( 2.4 ) = 2
CInt( 2.5 ) = 2 ***
CInt( 2.6 ) = 3
CInt 関数を使う。
CInt( -1.4 ) = -1
CInt( -1.5 ) = -2
CInt( -1.6 ) = -2
CInt( -2.4 ) = -2
CInt( -2.5 ) = -2 ***
CInt( -2.6 ) = -3
Int( -1.4 + 0.5 ) = -1
Int( -1.5 + 0.5 ) = -1
Int( -1.6 + 0.5 ) = -2
Int( -2.4 + 0.5 ) = -2
Int( -2.5 + 0.5 ) = -2
Int( -2.6 + 0.5 ) = -3
+0.5 してから Int 関数を使う。
Fix( -1.4 + 0.5 ) = 0
Fix( -1.5 + 0.5 ) = -1
Fix( -1.6 + 0.5 ) = -1
Fix( -2.4 + 0.5 ) = -1
Fix( -2.5 + 0.5 ) = -2
Fix( -2.6 + 0.5 ) = -2
(メモ) Fix 関数を使うと、誤差が大きい。
切り上げ
-Int( -(1.1) ) = 2
-Int( -(-1.1) ) = -1
マイナスして Int 関数を使い、マイナスする。
←
▼
▲
Sub main()
Dim folder, fnames()
Dim fname, f
Dim key_date
key_date = CDate( "2008/06/16" )
ExpandWildcard "sub\*", F_File Or F_SubFolder, folder, fnames
For Each fname in fnames
Set f = g_fs.GetFile( folder + "\" + fname )
If f.DateLastModified > key_date Then echo f.Path & " - " & f.DateLastModified
Next
End Sub
年月日時分秒の文字列を Date 型に変換します
現在の年月日時分秒を返します
Date 型の扱いの基本
ミリ秒単位の計測
タイムゾーン
Date型を文字列に変換します
関連
サンプル
←
▼
▲
文字列型に変換すると、地域に合わせたフォーマットの文字列を返します。
日本:
2008/06/11 14:28:01
文字列型への変換
文字列型から Date クラスへの変換
代入は Set 不要
Date 型はクラスではないので、Set d = CDate(...) のような Set は不要です。
参考
2008/06/11
= CStr( Date() )
= CStr( Now() )
= CStr( Time() )
14:28:01
t = CDate( "2008/06/16" )
西暦 100年 1月 1日〜 9999年 12月31日まで表せます。
Date 型の値のタイムゾーンは、その定義によります。
a = CDate("12:00") のとき、a がローカル・タイムゾーンの日時を格納するという定義であったら、
ローカル・タイムゾーンでの 12:00 になり、
b = CDate("3:00") のとき、b が UTC の日時を格納するという定義であったら、
UTC での 3:00 になります。
Date 型の値に、タイムゾーンの定義を追加したら、世界共通の絶対的な時刻になります。
Date 型のタイムゾーン
←
▼
▲
Function Now() as Date
現在の年月日時分秒を返します。
OSに設定している地域=「日本」なら、CStr( Now() ) は次のようになります。
2007/08/10 12:26:21
WScript.Echo "Now = " & Now()
関連
サンプル
→ TestableNow (vbslib)
←
▼
▲
Function Date() as Date
現在の年月日を返します。
OSに設定している地域=「日本」なら、CStr( Date() ) は次のようになります。
2007/08/10
時刻は、0:00:00 になります。
←
▼
▲
Function Time() as Date
現在の時分秒を返します。
OSに設定している地域=「日本」なら、CStr( Time() ) は次のようになります。
9:15:04
年月日は、1899年 12月 30日(土) になります。
←
▼
▲
Function Timer() as Single
今日の 0:00 から経過した時間を返します。(単位は秒、小数あり)
WScript.Echo Timer
サンプル:
出力例:
46529.59
精度は、約16ミリ秒のようです。(WinXP)
Dim i, t0 : t0 = Timer
For i=1 To 1000
WScript.Echo i & " " & (Timer - t0) & "(sec)"
Next
計測スクリプト:
:
56 0(sec)
57 0(sec)
58 0(sec)
59 0.015625(sec)
60 0.015625(sec)
61 0.015625(sec)
:
出力例:
Dim t0, t1
t0 = Timer()
WScript.Sleep 30
t1 = Timer() - t0
WScript.Echo FormatNumber( t1, 3 ) & "(sec)"
サンプル: t0= から t1= の間の時間を表示する
0.031(sec)
出力例:
←
▼
▲
Function CDate( Date as variant ) as Date
Date が文字列のときの例
CDate("July 1, 2008")
アメリカ:
CDate("2008/7/1")
日本:
年月日を Date 型に変換します。
CDate("2008/7/1 12:29")
時分秒が省略されると 0時、0分、0秒のように、各位の値が 0 になります。
年が省略されると、今年になります。
年月日が省略されると、1899年、12月、30日になります。
CDate("7/1 12:29")
2008/7/1 0:00:00
2008/7/1 12:29:00
(今年)/7/1 12:29:00
格納される値
CDate("12:29")
1899/12/30 12:29:00
#7/1 12:00# のように # で囲んだものは、リテラル日時です。
タイムゾーンは、その定義によります。
a = #12:00# のとき、a がローカル・タイムゾーンの日時を格納するという定義であったら、
ローカル・タイムゾーンでの 12:00 になり、
b = #3:00# のとき、b が UTC の日時を格納するという定義であったら、
UTC での 3:00 になります。
CDate("7/1")
(今年)/7/1 0:00:00
世界共通にするときは注意
日付リテラルや時刻リテラルか、DateSerial を使用してください。
←
▼
▲
Function DateSerial( Year as integer, Month as integer, Day as integer ) as Date
年月日から Date 型を返します。
←
▼
▲
Function FormatDateTime( aDateTime as Date [, NamedFormat as integer] ) as string
Date型を文字列に変換します。
vbGeneralDate
地域の日付か時刻、または両方を表示します。 CStr と同じです。
(デフォルト)
vbLongDate
vbLongTime
vbShortDate
vbShortTime
地域の日付(長い形式)
地域の日付(短い形式)
24 時間形式
地域の時刻
NamedFormat 引数
12:41
12:41:25
2010/12/03
2010年12月3日
2010/12/03 12:41:25
←
▼
▲
Function GetLocale() as integer
現在の地域を表す番号を返します。
←
▼
▲
Function Month( d as Date ) as integer
Function Day( d as Date ) as integer
Day("October 19, 1962") = 19
月
日
Function Hour( d as Date ) as integer
Function Minute( d as Date ) as integer
Function Second( d as Date ) as integer
Function Weekday( d as Date ) as integer
Function Year( d as Date ) as integer
vbSunday
1
vbMonday
vbSaturday
vbTuesday
vbWednesday
vbThursday
vbFriday
2
3
4
5
6
7
曜
年
時
分
11月なら 11
秒
0〜23
Function WeekdayName( w as integer ) as string
Function MonthName( Month as integer [, IsAbbreviate as boolean ] ) as string
1=日、2=月、…、7=土
←
▼
▲
If Now() >= CDate("2008/7/1 13:30") Then
'// 2008/7/1 13:30 以後なら
End If
比較するときは、Date 型同士を比較してください。
年が省略されると、今年になります。
時分秒が省略されると 0時、0分、0秒になります。
その日の終わりと比較するときは、その日ではなく次の日にするか、
時分(23:59:59) も指定してください。
年月日が省略されると、0年、0月、0日になります。
現在の時刻と比較するときは、現在の時刻を
で取得してください。
7/1
7/2
サンプル: 7/1が〆切のときに、〆切前かどうか
If Now() <= CDate("7/1 23:59:59") Then
If Now() < CDate("7/2") Then
← 厳密には1秒異なるが十分
サンプル: 昼休みかどうか
If Time() >= CDate("12:00") and Time() < CDate("13:00") Then
7/2
7/1
12:00
13:00
13:00
12:00
ただし、等しいかどうかの判定は、浮動小数型のように誤差を考慮してください。
If Abs(DateDiff("s", Now(), CDate("2008/7/1 13:30") )) < 1 Then
'// 現在が 2008/7/1 13:30 丁度なら
End If