Fordraiders
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("tblRepla ceDot")
For Each dbRow As Data.DataRow In dbDataSet.Tables("tblRepla ceDot").Ro ws
' regex call to replace dot
dbRow.Item("fldDescription ") = Regex.Replace(dbRow.Item(" fldDescrip tion"), _
"([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
What I have:
The following regex expression replaces a dot with a "<space>"
With dbDataSet.Tables("tblRepla
For Each dbRow As Data.DataRow In dbDataSet.Tables("tblRepla
' regex call to replace dot
dbRow.Item("fldDescription
"([a-zA-Z])(\.)([a-zA-Z])"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.OleDbDataAdapte r
Dim dbCommandBuilder As Data.OleDb.OleDbCommandBui lder
Dim dbDataSet As Data.DataSet
dbConnection = New Data.OleDb.OleDbConnection ("Provider =Microsoft .Jet.OLEDB .4.0;Data Source=c:\DM\MyData.mdb;Us er Id=admin;Password=;")
dbConnection.Open()
dbCommand = New Data.OleDb.OleDbCommand("S ELECT * FROM tblReplaceDot", dbConnection)
dbDataAdapter = New Data.OleDb.OleDbDataAdapte r(dbComman d)
dbCommandBuilder = New Data.OleDb.OleDbCommandBui lder(dbDat aAdapter)
dbDataSet = New Data.DataSet
dbDataAdapter.Fill(dbDataS et, "tblReplaceDot")
With dbDataSet.Tables("tblRepla ceDot")
For Each dbRow As Data.DataRow In dbDataSet.Tables("tblRepla ceDot").Ro ws
' regular fraction replace function
dbRow.Item("fldDescription ") = Regex.Replace(dbRow.Item(" fldDescrip tion"), _
"(\d{2,}/\d|\d+\s+\d/\d)", "$1 $3")
Next
End With
dbDataAdapter.Update(dbDat aSet, "tblReplaceDot")
dbConnection.Close()
MsgBox("Replace Fraction Completed", MsgBoxStyle.Information)
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.OleDbDataAdapte
Dim dbCommandBuilder As Data.OleDb.OleDbCommandBui
Dim dbDataSet As Data.DataSet
dbConnection = New Data.OleDb.OleDbConnection
dbConnection.Open()
dbCommand = New Data.OleDb.OleDbCommand("S
dbDataAdapter = New Data.OleDb.OleDbDataAdapte
dbCommandBuilder = New Data.OleDb.OleDbCommandBui
dbDataSet = New Data.DataSet
dbDataAdapter.Fill(dbDataS
With dbDataSet.Tables("tblRepla
For Each dbRow As Data.DataRow In dbDataSet.Tables("tblRepla
' regular fraction replace function
dbRow.Item("fldDescription
"(\d{2,}/\d|\d+\s+\d/\d)",
Next
End With
dbDataAdapter.Update(dbDat
dbConnection.Close()
MsgBox("Replace Fraction Completed", MsgBoxStyle.Information)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Fernando knows VB.NET better than I do--I can only supply some of the patterns.
ASKER
Thanks Very Much !
Not a Problem always glad to help. ;=)
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