2015年6月11日木曜日

正規表現(VB)

Imports System.Text
Imports System.Text.Encoding
Imports System.Text.RegularExpressions

Public Class Validate

    Public Function ConvertZenkakuToHankaku(ByVal str As String) As String
        Return Strings.StrConv(str, VbStrConv.Narrow) ' 数字、カタカナを半角に変換 C#にはないので、VisualBasicをインポートしてあげる
    End Function

    Public Function IsHankaku(ByVal str As String) As Boolean
        Dim Bytelen As Integer = System.Text.Encoding.GetEncoding("shift_jis").GetByteCount(str)
        Return str.Length = Bytelen
    End Function

    Public Overridable Function IsPostalCode(ByVal str As String) As Boolean
        Dim RegExp As New Regex("^[0-9]{3}-[0-9]{4}$")
        If RegExp.IsMatch(str) Then
            Return True
        Else
            Return False
        End If
    End Function


    Public Overridable Function IsNumber(ByVal str As String) As Boolean
        'http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?forum=7&topic=26783
        'Dim RegExp As New Regex("^[0-9]+$") '半角の数字のみ
        Dim RegExp As New Regex("^[-.0-9]+$") '半角の数字のみ'1.0 -1,-のみ なども含む
        If str.Substring(0, 1) = String.Empty Then
            Return False
        End If

        If str.Substring(0, 1) = "-" AndAlso str.Length = 1 Then
            Return False
        End If

        If RegExp.IsMatch(str) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function IsDateFormatBySlash(ByVal strDate As String) As Boolean
        Dim regExp As New Regex("^[0-9]{4}/[0-9]{2}/[0-9]{2}") '数字4/数字2/数字2
        If IsDate(strDate) Then
            If regExp.IsMatch(strDate) Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Public Function IsDateFormatByHaihun(ByVal strDate As String) As Boolean
        Dim regExp As New Regex("^[0-9]{4}-[0-9]{2}-[0-9]{2}") '数字4/数字2/数字2
        If IsDate(strDate) Then
            If regExp.IsMatch(strDate) Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

End Class




サルにもわかる正規表現入門
そのまま使える正規表現集 for regExp (javascript)
http://www.megasoft.co.jp/mifes/seiki/
http://www.geocities.co.jp/NatureLand/2023/reference/RegulerExpression/RegulerExpression03.html
http://qiita.com/Koo_zZ/items/7c8811b5cf37d700adc4

0 件のコメント:

コメントを投稿