Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

Regex expression needed for unique for fraction problem

vb.net 2003

What I have:
The following regex  expression replaces a dot with a "<space>"


With dbDataSet.Tables("tblReplaceDot")
            For Each dbRow As Data.DataRow In dbDataSet.Tables("tblReplaceDot").Rows
               
                ' regex call to replace dot
                dbRow.Item("fldDescription") = Regex.Replace(dbRow.Item("fldDescription"), _
                    "([a-zA-Z])(\.)([a-zA-Z])", "$1 $3")
            Next
        End With
----------------------------------------------------------------------------
What I need:
To look for patterns of fractions and replace with example
look for 11/4   replace with    1 1/4
         11 1/8"    replace with  11-1/8"
         21/2",     replace  with  2 1/2"
         22 1/4"   replace with   22-1/4"

You will notice I only
need a  -   "dash" ,  if the /  is preceded by  2 numerics a <space>  and numeric


Thanks
fordraiders
SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi fordraiders;

Because the Regex Replace method uses a simple string to replace a pattern you will need to use a replace function that Regex will call when it finds a match. The following code sample should work for you.

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim input As String = "The string to replace data in"
        Dim pattern As String = "(\d{2,}/\d|\d+\s+\d/\d)"
        Dim output As String = Regex.Replace(input, pattern, AddressOf StrReplace)

    End Sub

    Private Function StrReplace(ByVal m As Match) As String

        Dim str As String = m.Value
        Dim strRep As String = ""

        If str.IndexOf(" "c) >= 0 Then
            Dim idx As Integer = str.IndexOf(" "c)
            strRep = str.Substring(0, idx) & "-" & str.Substring(idx + 1)
        Else
            Dim idx As Integer = str.IndexOf("/"c)
            strRep = str.Substring(0, idx - 1) & " " & str.Substring(idx - 1)
        End If

        Return strRep

    End Function


Fernando
Avatar of Fordraiders

ASKER

Zyloch , Perfect...

What About  double digit numerator and denominator
example:
2211/16
115/32

Fernando Soto:

If I can figure out how to put your suggestion into my Access Table loop:
Can't seem to get this to work:
Dim input As String = "The string to replace data in"
        Dim pattern As String = "(\d{2,}/\d|\d+\s+\d/\d)"
        Dim output As String = Regex.Replace(input, pattern, AddressOf StrFractionReplace)

        Dim dbConnection As Data.OleDb.OleDbConnection
        Dim dbCommand As Data.OleDb.OleDbCommand
        Dim dbDataAdapter As Data.OleDb.OleDbDataAdapter
        Dim dbCommandBuilder As Data.OleDb.OleDbCommandBuilder
        Dim dbDataSet As Data.DataSet

        dbConnection = New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\DM\MyData.mdb;User Id=admin;Password=;")

        dbConnection.Open()
        dbCommand = New Data.OleDb.OleDbCommand("SELECT * FROM tblReplaceDot", dbConnection)
        dbDataAdapter = New Data.OleDb.OleDbDataAdapter(dbCommand)
        dbCommandBuilder = New Data.OleDb.OleDbCommandBuilder(dbDataAdapter)
        dbDataSet = New Data.DataSet

        dbDataAdapter.Fill(dbDataSet, "tblReplaceDot")

        With dbDataSet.Tables("tblReplaceDot")
            For Each dbRow As Data.DataRow In dbDataSet.Tables("tblReplaceDot").Rows
                ' regular fraction replace function
                 dbRow.Item("fldDescription") = Regex.Replace(dbRow.Item("fldDescription"), _
                    "(\d{2,}/\d|\d+\s+\d/\d)", "$1 $3")

               
            Next
        End With

        dbDataAdapter.Update(dbDataSet, "tblReplaceDot")

        dbConnection.Close()
        MsgBox("Replace Fraction Completed", MsgBoxStyle.Information)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fernando knows VB.NET better than I do--I can only supply some of the patterns.
Thanks Very Much !
Not a Problem always glad to help. ;=)