I have a text file containing lines, similar to these
000001 , Line 1 of text , customer 1 name
000002 , Line 2 of text , customer 2 name
000003 , Line 3 of text , customer 3 name
= = =
= = =
= = =
000087 , Line 87 of text, customer 87 name
= = =
= = =
001327 , Line 1327 of text, customer 1327 name
= = =
= = =
= = =
I can write a program that reads each line of the above file to convert it to the following format:
000001 , 1st Line , 1st Customer name
000002 , 2nd Line , 2nd Customer name
000003 , 3rd Line , 3rd Customer name
= = =
= = =
= = =
000087 , 87th Line, 87th Customer name
= = =
= = =
001327 , 1327th Line, 1327th Customer name
= = =
= = =
= = =
My Question: is there a straight forward method to achieve the same output using Regular expression?
I tried the following:
Dim pattern As String = "(\d{6}) , (Line \d+ of text) , (customer \d name)"
Dim replacement As String = " $1 , $2 Line , $3 Customer name "
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(my_input_file, replacement)
but the result is far from the desired output.
Please help
Your regex captures too much. The groups should capture only digits:
Dim pattern As String = "(\d{6}) , Line (\d+) of text , customer (\d+) name"
Also, as you want to replace the numbers with ordinal numbers, you should rather use String.Format
to do the formatting (line by line):
Dim match as Match = rgx.match(my_input_file_line)
Dim outputLine as String = String.Format(" {0} , {1} Line , {2} Customer name", _
m.Groups(1).Value, GetOrdinal(m.Groups(2).Value), GetOrdinal(m.Groups(3).Value))
where GetOrdinal
is a method that changes a string for number to an ordinal number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With