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

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.txt"

        Dim sr As New System.IO.StreamReader(fileName)
        Dim lines() As String = Split(sr.ReadToEnd.Trim.TrimEnd(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(fileName)
        sw.WriteLine(String.Join(vbCrLf, lines))
        sw.Close()

Thanks
fordraiders

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Would you like to use a regular expression?

Bob
Avatar of Fordraiders

ASKER

I need a procedure..Like this one
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
ok..I will trust you guys judgement..
Imports System.Text.RegularExpressions

' 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
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland 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
      Dim fileName As String = "C:\Samples\ReplaceTest.txt"

        Dim sr As New System.IO.StreamReader(fileName)
        Dim lines() As String = Split(sr.ReadToEnd.Trim.TrimEnd(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(fileName)
        sw.WriteLine(String.Join(vbCrLf, lines))
        sw.Close()

'===================================================
Is this what you mean:  I have to pass my  "input"  as Filename ?




sorry tim and  learnedone....
I was doing some reading and just suggestions
Thanks, give me a min to soak in...
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 think this is it ?

Regular Expression Language Elements

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
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
Wish I had more to split..
But that was super !
Tim, LearnedOne..
Why Would I need to totally delare this

        Dim re As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)

In the Assembly info I have:
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Data.OleDb
Imports System.Text.RegularExpressions

Thanks
fordraiders