RegularExpression을 사용하여 Replace, Search, 패턴검사를 쉽게 할수 있다.
' 정규식을 사용한 우편번호 패턴채크
Dim regex As System.Text.RegularExpressions.Regex = _
New System.Text.RegularExpressions.Regex("^\d{3}-\d{3}$")
If regex.IsMatch(Me.txtPostNo.Text) Then
MessageBox.Show("올바른 우편번호")
End If
전자메일 주소 확인
Public Class Tester
Public Shared Sub Main
Dim testString As String
Dim emailPattern As String = _
"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@" & _
"([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$"
testString = "a@nowhere.com"
Console.WriteLine(testString & Space(3) & _
Regex.IsMatch(testString, emailPattern))
testString = "a@doe@mybad.com"
Console.WriteLine(testString & Space(3) & _
Regex.IsMatch(testString, emailPattern))
End Sub
End Class
-->
a@nowhere.com True
a@doe@mybad.com False
미국 우편번호 매칭
Public Shared Sub Main
Dim sPostalCode As String = "12345-1111"
Dim sPattern As String
Dim objRegEx As Regex
sPattern = "\d\d\d\d\d-\d\d\d\d"
Console.WriteLine(objRegEx.IsMatch(sPostalCode, sPattern))
End Sub
-->
True
"," ";" "공백" 을 한꺼번에 Split 하는 방법 입니다..
txtInput1.Text = "aa,bb;cc dd"
Dim options As RegexOptions = RegexOptions.None
'정규식 지정
Dim regex As Regex = New Regex(",|\s|;", options)
Dim results() As String = regex.Split(txtInput1.Text)
Me.lblResult1.Text = ""
Dim result As String
For Each result In results
Me.lblResult1.Text += result + vbCrLf
Next
결과값 :
aa
bb
cc
dd
'Match and extract numbers
Imports System.Text.RegularExpressions
Public Class Tester
Public Shared Sub Main
Dim source As String = _
"This 321.0 string -0.020 contains " & _
"3.0E-17 several 1 2. 34 numbers"
Dim parser As New _
Regex("[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?")
Dim sourceMatches As MatchCollection = parser.Matches(source)
Dim counter As Integer
Console.WriteLine(sourceMatches.Count.ToString() & vbNewLine)
For counter = 0 To sourceMatches.Count - 1
Console.WriteLine(sourceMatches(counter).Value.ToString())
Console.WriteLine(CDbl(sourceMatches(counter).Value).ToString())
Next counter
End Sub
End Class
-->
6
321.0
321
-0.020
-0.02
3.0E-17
3E-17
1
1
2
2
34
34
'Word count
Public Class Tester
Public Shared Sub Main
Dim quote As String = "q d e w "
Do While (quote.IndexOf(Space(2)) >= 0)
quote = quote.Replace(Space(2), Space(1))
Loop
Dim wordCount As Integer = Split(quote, Space(1)).Length
Console.WriteLine(quote & vbNewLine & "Number of words: " & wordCount.ToString)
End Sub
End Class
-->
q d e w
Number of words: 5
Public Class Tester
Public Shared Sub Main
Dim quote As String = "The important thing is not to " & _
"stop questioning. --Albert Einstein"
Dim parser As New Regex("\w+")
Dim totalMatches As Integer = parser.Matches(quote).Count
Console.WriteLine(quote & vbNewLine & "Number words: " & _
totalMatches.ToString)
End Sub
End Class
-->
The important thing is not to stop questioning. --Albert Einstein
Number words: 10
'Count chars
Public Class Tester
Public Shared Sub Main
Dim quote As String = _
"The important thing" & vbNewLine & _
"is not to stop questioning." & vbNewLine & _
"--Albert Einstein" & vbNewLine
Dim numChars As Integer = Regex.Matches(quote, ".").Count
Console.WriteLine(numChars)
End Sub
End Class
-->
66
'Word count
Public Class Tester
Public Shared Sub Main
Dim quote As String = _
"The important thing" & vbNewLine & _
"is not to stop questioning." & vbNewLine & _
"--Albert Einstein" & vbNewLine
Dim numWords As Integer = Regex.Matches(quote, "\w+").Count
Console.WriteLine(numWords)
End Sub
End Class
-->
10
'Count line
Public Class Tester
Public Shared Sub Main
Dim quote As String = _
"The important thing" & vbNewLine & _
"is not to stop questioning." & vbNewLine & _
"--Albert Einstein" & vbNewLine
Dim numLines As Integer = Regex.Matches(quote, ".+\n*").Count
Console.WriteLine(numLines)
End Sub
End Class
-->
3
'Using Regex method Replace: substituted for *
Public Class Tester
Public Shared Sub Main
Console.WriteLine(Regex.Replace("This sentence ends in 5 stars *****", "\*", ""))
End Sub
End Class
-->
This sentence ends in 5 stars ^^^^^
'Replace one string with another
Public Class Tester
Public Shared Sub Main
Console.WriteLine(New Regex("stars").Replace("This sentence ends in 5 stars *****", _
"carets"))
End Sub
End Class
-->
This sentence ends in 5 carets *****
'Every word replaced by another word
Public Class Tester
Public Shared Sub Main
Console.WriteLine(Regex.Replace("This sentence ends in 5 stars *****", "\w+", "word"))
End Sub
End Class
-->
word word word word word word *****
'Replace first 3 digits
Public Class Tester
Public Shared Sub Main
Console.WriteLine(New Regex("\d").Replace("1, 2, 3, 4, 5, 6, 7, 8", "digit", 3))
End Sub
End Class
-->
digit, digit, digit, 4, 5, 6, 7, 8
'Regex.CompileToAssembly
Public Class Tester
Public Shared Sub Main
Dim numPattern As String = _
"[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?"
Dim wordPattern As String = "\w+"
Dim whichNamespace As String = "NumbersRegex"
Dim isPublic As Boolean = True
Dim compNumbers As New RegexCompilationInfo(numPattern, _
RegexOptions.Compiled, "RgxNumbers", _
whichNamespace, isPublic)
Dim compWords As New RegexCompilationInfo(wordPattern, _
RegexOptions.Compiled, "RgxWords", whichNamespace, _
isPublic)
Dim compAll() As RegexCompilationInfo = _
{compNumbers, compWords}
Dim whichAssembly As New _
System.Reflection.AssemblyName("RgxNumbersWords")
Regex.CompileToAssembly(compAll, whichAssembly)
End Sub
End Class
'Match regular expression to string and print out all matches
Module Tester
Sub Main()
Dim myMatch As Match
Dim expression As Regex = New Regex("J.*\d[0-35-9]-\d\d-\d\d")
Dim string1 As String = "11-11-75" & _
vbCrLf & "is 11-05-68" & vbCrLf & _
"asdf 04-18-73" & vbCrLf & _
"fdsa 12-27-77"
For Each myMatch In expression.Matches(string1)
Console.WriteLine(myMatch.ToString())
Next
End Sub
End Module
http://www.java2s.com/Tutorial/VB/0360__Regular-Expressions/Catalog0360__Regular-Expressions.htm