I am trying to remove the line numbers of a text document.

Example:
N10 G01 F720 X0Y0Z0
N20 X0Y0Z0
etc...
N9000 X0Y0Z0
M0

I can remove the line number on 1 line only but need to do this throughout the entire document.

Private Sub Command1_Click()

WordFoundFlag = 0
For i = 1 To Len(Text1)
Char = Mid(Text1, i, 1)
If Char Like "[A-Za-z-]" And WordFoundFlag = 0 Then
WordFoundFlag = 1
StartPos = i
ElseIf Char Like "[!A-Za-z-]" And WordFoundFlag = 1 Then
ElseIf Char Like "[!0-9]" And WordFoundFlag = 1 Then
GoTo BYE
End If
Next

BYE:

FindFirstWord = Mid(Text1, StartPos, i - StartPos)
Text1 = Replace(Text1, FindFirstWord, "")

End Sub

Dani AI

Generated

A few points to close the gap between the replies: is right that you must handle every line instead of exiting after the first match, and the reason 's routine only removes one line number is that it stops once it finds the first match. A simpler, more robust approach is to treat the file (or the multiline TextBox contents) as text and apply a single multiline regular‑expression replace that strips leading line numbers on every line.

The pattern below removes an optional leading whitespace, an uppercase or lowercase N followed by digits, the word boundary after the digits, and any following spaces — only at the start of each line. In VB6 you can use the VBScript RegExp object (late binding) and set MultiLine = True so ^ matches each line start:

' VB6 (late binding)
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^\s*N\d+\b\s*"
re.IgnoreCase = True
re.Global = True
re.MultiLine = True

' load your file into allText (or use Text1.Text)
cleanText = re.Replace(allText, "")
' save cleanText back

In VB.NET the same idea uses System.Text.RegularExpressions:

' VB.NET
Dim pattern As String = "^\s*N\d+\b\s*"
Dim cleaned As String = Regex.Replace(inputText, pattern, "", RegexOptions.IgnoreCase Or RegexOptions.Multiline)

Notes and troubleshooting: test on a copy of the file first; if some lines have numbers without an N prefix, change the pattern to allow that (for VB.NET you can use ^\s*(?:N)?\d+\b\s*). If you prefer line-by-line processing, split the text into lines (e.g., Split(Text1.Text, vbCrLf) or TextBox.Lines) and apply the same replace per line. This avoids fragile character-by-character loops and makes the operation predictable for large files.

Recommended Answers

All 4 Replies

Naturally, it will remove only from the first line, because after your label BYE the program encounters the End Sub and exits.

There hast to be an outer loop that reads every line of text from your text document, and within that, your program has to find the line number.

If line numbers are in the beginning of every line and they are seperated by a space with the subsequent data you have to see only the first occurance of a blank space.

regards
AV Manoharan

ok, can you give me an idea of how to do this ?
i know what i want to do.

remove until space but everything that i have tried went really bad.

thank you
les

I will sight a very simlpe example.
If you are using ordinary files then
Open two files. one file having the searial nos in INPUT mode. and another without that in OUTPUT mode. OK

Open "Path\filename of your original text document" For Input As #1
Open "Path\filename of the text document without serial Nos" For Output As #2


Dim Str1,Str2 as String
Dim i, pos, TLength As Integer

Do While Not EOF(1)
Line Input #1, Str1
TLength=len(str1)
pos=Instr(str1," ")
Str2=mid(str1,pos+1,Tlength-pos)
Line Output #2 Str2
Loop
close #1
Close #2

Thank You

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.