14.3.4 查找嵌套标记

 

代码
    Sub FindNestedHtmlTags()
        
' Find all nested HTML tags in a file. (eg. <table>...</table>)
        Dim text As String = File.ReadAllText("test.htm")
        
Dim re As New Regex("<(?<tag>(table|tr|td|div|span))[\s>]", RegexOptions.IgnoreCase)
        
For Each m As Match In re.Matches(text)
            
' We've found an open tag. Lets look for open & close versions of this tag.
            Dim tag As String = m.Groups("tag").Value
            
Dim openTags As Integer = 1
            
Dim pattern2 As String = String.Format("((?<open><{0})[\s>]|(?<close>)</{0}>)", tag)
            
Dim found As String = Nothing
            
Dim re2 As New Regex(pattern2, RegexOptions.IgnoreCase)

            
For Each m2 As Match In re2.Matches(text, m.Index + 1)
                
If m2.Groups("open").Success Then
                    openTags 
+= 1
                
ElseIf m2.Groups("close").Success Then
                    openTags 
-= 1
                    
If openTags = 0 Then
                        found 
= text.Substring(m.Index, m2.Index + m.Length + 1 - m.Index)
                        
Exit For
                    
End If
                
End If
            
Next
            
' Display this match.
            If found IsNot Nothing Then
                Console.WriteLine(found)
                Console.WriteLine(
"------------------------------------------")
            
Else
                Console.WriteLine(
"Unmatched tag {0} at index {1}", tag, m.Index)
            
End If
        
Next
    
End Sub

 

 

14.3.5 分析数据文件

  

代码
Imports System.Text.RegularExpressions
Imports System.IO

Module ParsingDataFiles
    
Sub ParsingFixedWidthDataFiles()
        
Dim text As String =
            
"John  Smith   New York" & vbCrLf &
            
"Ann   Doe     Los Angeles" & vbCrLf
        
Dim seps() As String = {vbCrLf}
        
Dim lines() As String = text.Split(seps, StringSplitOptions.RemoveEmptyEntries)

        
Dim pattern As String = "^(?<first>.{6})(?<last>.{8})(?<city>.+)$"
        
Dim re As New Regex(pattern)
        
For Each line As String In lines
            
Dim m As Match = re.Match(line)
            Console.WriteLine(
"First={0}, Last={1}, City={2}",
                              m.Groups(
"first").Value.TrimEnd(),
                              m.Groups(
"last").Value.TrimEnd(),
                              m.Groups(
"city").Value.TrimEnd())
        
Next
    
End Sub

    
'分析不带引号的字符串
    Sub ParsingDelimitedDataFiles()
        
Dim text As String =
            
"John , Smith, New York" & vbCrLf &
            
"Ann, Doe, Los Angeles" & vbCrLf
        
Dim seps() As String = {vbCrLf}
        
Dim lines() As String = text.Split(seps, StringSplitOptions.RemoveEmptyEntries)

        
Dim pattern As String = "^\s*(?<first>.*?)\s*,\s*(?<last>.*?)\s*,\s*(?<city>.*?)\s*$"
        
Dim re As New Regex(pattern)
        
For Each line As String In lines
            
Dim m As Match = re.Match(line)
            Console.WriteLine(
"First={0}, Last={1}, City={2}",
                              m.Groups(
"first").Value.TrimEnd(),
                              m.Groups(
"last").Value.TrimEnd(),
                              m.Groups(
"city").Value.TrimEnd())
        
Next
    
End Sub

    
'分析带引号的字符串
    Sub ParsingDelimitedQuotedDataFiles()
        
Dim text As String =
            
"'John, P.' , ""Smith"" , ""New York""" & vbCrLf &
            
"'Robert ""Slim""', """" , ""Los Angeles, CA""" & vbCrLf
        
Dim seps() As String = {vbCrLf}
        
Dim lines() As String = text.Split(seps, StringSplitOptions.RemoveEmptyEntries)

        
Dim pattern As String =
            
"^\s*(?<q1>[""'])(?<first>.*?)\k<q1>\s*,\s*(?<q2>[""'])(?<last>.*?)\k<q2>\s*,\s*(?<q3>[""'])(?<city>.*?)\k<q3>\s*$"

        
Dim re As New Regex(pattern)
        
For Each line As String In lines
            
Dim m As Match = re.Match(line)
            Console.WriteLine(
"First={0}, Last={1}, City={2}",
                              m.Groups(
"first").Value.TrimEnd(),
                              m.Groups(
"last").Value.TrimEnd(),
                              m.Groups(
"city").Value.TrimEnd())
        
Next
    
End Sub

    
'只有当该字段包含有分隔符字符时才将文本字段放在引号内
    Sub ParsingDelimitedQuotedDataFiles2()
        
Dim text As String =
            
"""John, P."" , Doe , ""Los Angeles, CA""" & vbCrLf &
            
"Robert, Smith, New York" & vbCrLf
        
Dim seps() As String = {vbCrLf}
        
Dim lines() As String = text.Split(seps, StringSplitOptions.RemoveEmptyEntries)

        
Dim pattern As String =
            
"^\s*(?<q1>[""']?)(?<first>.*?)(?(q1)\k<q1>)\s*,\s*(?<q2>[""']?)(?<last>.*?)(?(q2)\k<q2>)\s*,\s*(?<q3>[""']?)(?<city>.*?)(?(q3)\k<q3>)\s*$"

        
Dim re As New Regex(pattern)
        
For Each line As String In lines
            
Dim m As Match = re.Match(line)
            Console.WriteLine(
"First={0}, Last={1}, City={2}",
                              m.Groups(
"first").Value.TrimEnd(),
                              m.Groups(
"last").Value.TrimEnd(),
                              m.Groups(
"city").Value.TrimEnd())
        
Next
    
End Sub
End Module

 

 

14.3.6 分析和计算表达式

 

14.3.7 分析代码

 

14.3.8 使用正则表达式

 

代码
    Sub PlayPoker()
        PlayPokerHand(
"StraightFlush""1H""2H""3H""4H""5H")
        PlayPokerHand(
"StraightFlush""3H""4H""5H""6H""7H")
        PlayPokerHand(
"StraightFlush""6C""7C""8C""9C""TC")
        PlayPokerHand(
"StraightFlush""7S""8S""9S""TS""JS")
        PlayPokerHand(
"StraightFlush""8S""9S""TS""JS""QS")
        PlayPokerHand(
"StraightFlush""TD""JD""QD""KD""1D")

        PlayPokerHand(
"Flush""8S""JS""QS""KS""1S")
        PlayPokerHand(
"Flush""7H""8H""9H""TH""KH")
        PlayPokerHand(
"Flush""8C""9C""TC""QC""KC")

        PlayPokerHand(
"FourOfAKind""8C""9D""8D""8H""8S")
        PlayPokerHand(
"FourOfAKind""TS""TC""QD""TD""TH")
        PlayPokerHand(
"FourOfAKind""1C""1S""8S""1H""1D")

        PlayPokerHand(
"FullHouse""1C""1D""8C""1D""8S")
        PlayPokerHand(
"FullHouse""9C""9S""8D""8S""9H")
        PlayPokerHand(
"FullHouse""QC""TD""QD""TS""QS")

        PlayPokerHand(
"Straight""3S""7S""5S""4S""6D")
        PlayPokerHand(
"Straight""6D""7S""8H""9H""TD")
        PlayPokerHand(
"Straight""7S""8D""9D""TH""JC")
        PlayPokerHand(
"Straight""TC""JS""QS""KS""1S")

        PlayPokerHand(
"ThreeOfAKind""9C""9S""8H""TD""9D")
        PlayPokerHand(
"ThreeOfAKind""TC""TS""7S""TH""1D")
        PlayPokerHand(
"ThreeOfAKind""TC""TS""8S""TH""KH")

        PlayPokerHand(
"TwoPairs""1C""1F""8F""DP""8Q")
        PlayPokerHand(
"TwoPairs""9C""KF""8F""8P""9Q")
        PlayPokerHand(
"TwoPairs""1C""DF""QF""DP""QQ")

        PlayPokerHand(
"OnePair""1C""1H""KH""TS""8S")
        PlayPokerHand(
"OnePair""9C""KH""QH""8S""9D")
        PlayPokerHand(
"OnePair""1S""TS""QC""8C""QC")

        PlayPokerHand(
"HighCard""1C""QC""KC""TD""8D")
        PlayPokerHand(
"HighCard""TC""KC""QD""8D""9H")
        PlayPokerHand(
"HighCard""1C""TC""KD""8D""QH")
        PlayPokerHand(
"HighCard""8D""QD""TD""9D""KH")
    
End Sub

    
Private Sub PlayPokerHand(ByVal expectedScore As StringByVal ParamArray cards() As String)
        
Dim score As String = PokerGame.EvalPokerScore2(cards)
        Console.WriteLine(
"{0} {1} {2} {3} {4} => {5}", cards(0), cards(1), cards(2), cards(3), cards(4), score)
        
If score <> expectedScore Then Console.WriteLine("    WRONG! (expected {0})", expectedScore)
    
End Sub