201 KAR 2:340. ...">

Low Row Manipulation

I have the following line that I want to extract from the following parts:

<FONT COLOR="GREEN">201 KAR 2:340.</FONT>

In this particular case, I want to extract the numbers 201.2 and 340, which I will use later for concatenation to form another line:

http://www.lrc.state.ky.us/kar/201/002/340reg.htm

I have a solution, but it is not easy to read, and it seems rather awkward. It includes the use of the mid function. There he is:

intTitle = CInt(Mid(strFontTag, 
                    InStr(strFontTag, ">") + 1, 
                    (InStr(strFontTag, "KAR") - InStr(strFontTag, ">")) 
                           - 3))

I would like to know, maybe there is a better way to approach this task. I understand that I can write some descriptive variable names, like intPosOfEndOfOpeningFontTag, to describe what the first InStr function does, but it still seems awkward to me.

- split regex - , ? , , . .

+5
4

: <FONT[^>]*>.*?(\d+).*?(\d+).*?(\d+).*?<\/FONT>

+1
<FONT[^>]*>[^\d]*(\d+)[^\d]*(\d+):(\d+)[^\d]*</FONT>
+1

Class

Imports System
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Xml.Linq
Imports System.Linq

Public Class clsTester
    'methods
    Public Sub New()
    End Sub

    Public Function GetTitleUsingRegEx(ByVal fpath$) As XElement
        'use this function if your input string is not a well-formed
        Dim result As New XElement(<result/>)
        Try
            Dim q = Regex.Matches(File.ReadAllText(fpath), Me.titPattern1, RegexOptions.None)
            For Each mt As Match In q
                Dim t As New XElement(<title/>)
                t.Add(New XAttribute("name", mt.Groups("name").Value))
                t.Add(New XAttribute("num1", mt.Groups("id_1").Value))
                t.Add(New XAttribute("num2", mt.Groups("id_2").Value))
                t.Add(New XAttribute("num3", mt.Groups("id_3").Value))
                t.Add(mt.Value)
                result.Add(t)
            Next mt
            Return result
        Catch ex As Exception
            result.Add(<error><%= ex.ToString %></error>)
            Return result
        End Try
    End Function

    Public Function GetTitleUsingXDocument(ByVal fpath$) As XElement
        'use this function if your input string is well-formed
        Dim result As New XElement(<result/>)
        Try
            Dim q = XElement.Load(fpath).Descendants().Where(Function(c) Regex.IsMatch(c.Name.LocalName, "(?is)^font$")).Where(Function(c) Regex.IsMatch(c.Value, Me.titPattern2, RegexOptions.None))
            For Each nd As XElement In q
                Dim s = Regex.Match(nd.Value, Me.titPattern2, RegexOptions.None)
                Dim t As New XElement(<title/>)
                t.Add(New XAttribute("name", s.Groups("name").Value))
                t.Add(New XAttribute("num1", s.Groups("id_1").Value))
                t.Add(New XAttribute("num2", s.Groups("id_2").Value))
                t.Add(New XAttribute("num3", s.Groups("id_3").Value))
                t.Add(nd.Value)
                result.Add(t)

            Next nd
            Return result
        Catch ex As Exception
            result.Add(<error><%= ex.ToString %></error>)
            Return result
        End Try
    End Function

    'fields
    Private titPattern1$ = "(?is)(?<=<font[^<>]*>)(?<id_1>\d+)\s+(?<name>[a-z]+)\s+(?<id_2>\d+):(?<id_3>\d+)(?=\.?</font>)"
    Private titPattern2$ = "(?is)^(?<id_1>\d+)\s+(?<name>[a-z]+)\s+(?<id_2>\d+):(?<id_3>\d+)\.?$"
End Class

Using

Sub Main()
        Dim y = New clsTester().GetTitleUsingRegEx("C:\test.htm")
        If y.<error>.Count = 0 Then
            Console.WriteLine(String.Format("Result from GetTitleUsingRegEx:{0}{1}", vbCrLf, y.ToString))
        Else
            Console.WriteLine(y...<error>.First().Value)
        End If

        Console.WriteLine("")
        Dim z = New clsTester().GetTitleUsingXDocument("C:\test.htm")

        If z.<error>.Count = 0 Then
            Console.WriteLine(String.Format("Result from GetTitleUsingXDocument:{0}{1}", vbCrLf, z.ToString))
        Else
            Console.WriteLine(z...<error>.First().Value)
        End If
        Console.ReadLine()
    End Sub

Hope this helps.

+1
source

I think that Jean-Francois Corbett is right.

Hide it in functions and never look back

Change the code as follows:

intTitle = GetCodesFromColorTag("<FONT COLOR="GREEN">201 KAR 2:340.</FONT>")

Create a new function:

Public Function GetCodesFromColorTag(FontTag as String) as Integer

    Return CInt(Mid(FontTag, InStr(FontTag, ">") + 1, 
                (InStr(FontTag, "KAR") - InStr(FontTag, ">")) 
                - 3))

End Function
0
source

All Articles