Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Regular Expression to convert String to different substring?

Tags:

regex

vb.net

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?

enter image description here

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

like image 387
Ahmad Avatar asked Feb 14 '23 15:02

Ahmad


1 Answers

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.

like image 166
Szymon Avatar answered May 07 '23 01:05

Szymon