Fordraiders
asked on
Revise procedure to Search and replace .
Vb.Net 2003
What I have:
MyData.txt file
3 columns of data : seperted by a Pipe Delimiter
LAWSON |12324R5|DRILLBIT station.bit standard length
LAWSON |32DE45|DRILLBIT remote.bit long length
UNION BUTTERFIELD|54667| 1.2 A.2 inches in length ' <--- the code should leave this . alone because it has a numeric to left and/or right
etc......
========================== ========== =========
What I need:
To revise this procedure
I now need this procedure to look for a . <------ dot or period
If it finds a dot AND it is surronded by an alpha character to the left AND to the right.
DELETE the period/dot , If not leave the line alone
' current routine ************************** ********
'===== this routine here is looking for double quotes and replacing
' =====if it finds a numeric to the left it replaces the double quotes
'===== with inch
Dim fileName As String = "C:\Samples\ReplaceTest.tx t"
Dim sr As New System.IO.StreamReader(fil eName)
Dim lines() As String = Split(sr.ReadToEnd.Trim.Tr imEnd(vbCr ).TrimEnd( vbLf), vbCrLf)
sr.Close()
Dim sb As System.Text.StringBuilder
Dim fields() As String
For i As Integer = 0 To lines.GetUpperBound(0)
fields = lines(i).Split("|")
' chr(34) = double quotes
If fields.GetUpperBound(0) >= 2 Then
sb = New System.Text.StringBuilder( fields(2))
For x As Integer = 1 To sb.Length - 1
If sb.Chars(x) = Chr(34) Then
If Char.IsDigit(sb.Chars(x - 1)) Then
sb.Remove(x, 1)
sb.Insert(x, " inch")
End If
End If
Next
fields(2) = sb.ToString
End If
lines(i) = String.Join("|", fields)
Next
Dim sw As New System.IO.StreamWriter(fil eName)
sw.WriteLine(String.Join(v bCrLf, lines))
sw.Close()
Thanks
fordraiders
What I have:
MyData.txt file
3 columns of data : seperted by a Pipe Delimiter
LAWSON |12324R5|DRILLBIT station.bit standard length
LAWSON |32DE45|DRILLBIT remote.bit long length
UNION BUTTERFIELD|54667| 1.2 A.2 inches in length ' <--- the code should leave this . alone because it has a numeric to left and/or right
etc......
==========================
What I need:
To revise this procedure
I now need this procedure to look for a . <------ dot or period
If it finds a dot AND it is surronded by an alpha character to the left AND to the right.
DELETE the period/dot , If not leave the line alone
' current routine **************************
'===== this routine here is looking for double quotes and replacing
' =====if it finds a numeric to the left it replaces the double quotes
'===== with inch
Dim fileName As String = "C:\Samples\ReplaceTest.tx
Dim sr As New System.IO.StreamReader(fil
Dim lines() As String = Split(sr.ReadToEnd.Trim.Tr
sr.Close()
Dim sb As System.Text.StringBuilder
Dim fields() As String
For i As Integer = 0 To lines.GetUpperBound(0)
fields = lines(i).Split("|")
' chr(34) = double quotes
If fields.GetUpperBound(0) >= 2 Then
sb = New System.Text.StringBuilder(
For x As Integer = 1 To sb.Length - 1
If sb.Chars(x) = Chr(34) Then
If Char.IsDigit(sb.Chars(x - 1)) Then
sb.Remove(x, 1)
sb.Insert(x, " inch")
End If
End If
Next
fields(2) = sb.ToString
End If
lines(i) = String.Join("|", fields)
Next
Dim sw As New System.IO.StreamWriter(fil
sw.WriteLine(String.Join(v
sw.Close()
Thanks
fordraiders
ASKER
I need a procedure..Like this one
I will be running these seperately later on..
I will be running these seperately later on..
Hi David,
Regular expressions is certainly the way to go with this, it will simplify it greatly and be significantly easier to modify in future.
Tim Cottee
Regular expressions is certainly the way to go with this, it will simplify it greatly and be significantly easier to modify in future.
Tim Cottee
ASKER
ok..I will trust you guys judgement..
Imports System.Text.RegularExpress ions
' Look for one alpha character + period + alpha character.
Dim pattern As String = "[A-Za-z]\.[A-Za-z]"
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
' Remove the period
sb.Remove(match.Index + 1, 1)
End If
Bob
' Look for one alpha character + period + alpha character.
Dim pattern As String = "[A-Za-z]\.[A-Za-z]"
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
' Remove the period
sb.Remove(match.Index + 1, 1)
End If
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dim fileName As String = "C:\Samples\ReplaceTest.tx t"
Dim sr As New System.IO.StreamReader(fil eName)
Dim lines() As String = Split(sr.ReadToEnd.Trim.Tr imEnd(vbCr ).TrimEnd( vbLf), vbCrLf)
sr.Close()
Dim sb As System.Text.StringBuilder
Dim fields() As String
Dim pattern As String = "[A-Za-z]\.[A-Za-z]"
For i As Integer = 0 To lines.GetUpperBound(0)
fields = lines(i).Split("|")
' chr(34) = double quotes
If fields.GetUpperBound(0) >= 2 Then
sb = New System.Text.StringBuilder( fields(2))
For x As Integer = 1 To sb.Length - 1
Dim match As Match = Regex.Match(filename, pattern) ' <--- your code here ?
If match.Success Then
' Remove the period
sb.Remove(match.Index + 1, 1)
End If
End If
Next
fields(2) = sb.ToString
End If
lines(i) = String.Join("|", fields)
Next
Dim sw As New System.IO.StreamWriter(fil eName)
sw.WriteLine(String.Join(v bCrLf, lines))
sw.Close()
'========================= ========== ========== ======
Is this what you mean: I have to pass my "input" as Filename ?
Dim sr As New System.IO.StreamReader(fil
Dim lines() As String = Split(sr.ReadToEnd.Trim.Tr
sr.Close()
Dim sb As System.Text.StringBuilder
Dim fields() As String
Dim pattern As String = "[A-Za-z]\.[A-Za-z]"
For i As Integer = 0 To lines.GetUpperBound(0)
fields = lines(i).Split("|")
' chr(34) = double quotes
If fields.GetUpperBound(0) >= 2 Then
sb = New System.Text.StringBuilder(
For x As Integer = 1 To sb.Length - 1
Dim match As Match = Regex.Match(filename, pattern) ' <--- your code here ?
If match.Success Then
' Remove the period
sb.Remove(match.Index + 1, 1)
End If
End If
Next
fields(2) = sb.ToString
End If
lines(i) = String.Join("|", fields)
Next
Dim sw As New System.IO.StreamWriter(fil
sw.WriteLine(String.Join(v
sw.Close()
'=========================
Is this what you mean: I have to pass my "input" as Filename ?
ASKER
sorry tim and learnedone....
I was doing some reading and just suggestions
Thanks, give me a min to soak in...
I was doing some reading and just suggestions
Thanks, give me a min to soak in...
ASKER
ok...so I can now get my expressions together and place them how I want them passing/looping my variables in one big swoop, by using these regular expressions?
I was looking at my msdn... where do I learn these expression values...?
"(?<N>\d)(?<Q>"")", "${N} inch") 'This replaces a " with inch
Thanks for all the help !
I was looking at my msdn... where do I learn these expression values...?
"(?<N>\d)(?<Q>"")", "${N} inch") 'This replaces a " with inch
Thanks for all the help !
ASKER
I think this is it ?
Regular Expression Language Elements
Regular Expression Language Elements
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
David,
I use expresso (http://www.ultrapico.com/Expresso.htm) to build and test them before sticking it into visual studio. It will even build the code for you!
Tim
I use expresso (http://www.ultrapico.com/Expresso.htm) to build and test them before sticking it into visual studio. It will even build the code for you!
Tim
ASKER
Wish I had more to split..
But that was super !
But that was super !
ASKER
Tim, LearnedOne..
Why Would I need to totally delare this
Dim re As System.Text.RegularExpress ions.Regex = New System.Text.RegularExpress ions.Regex (System.Te xt.Regular Expression s.RegexOpt ions.Compi led Or System.Text.RegularExpress ions.Regex Options.Ig noreCase Or System.Text.RegularExpress ions.Regex Options.Mu ltiline)
In the Assembly info I have:
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServ ices
Imports System.IO
Imports System.Data.OleDb
Imports System.Text.RegularExpress ions
Thanks
fordraiders
Why Would I need to totally delare this
Dim re As System.Text.RegularExpress
In the Assembly info I have:
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServ
Imports System.IO
Imports System.Data.OleDb
Imports System.Text.RegularExpress
Thanks
fordraiders
Bob