how to parse long string and add line break

I have a long field that contains a string like this one:

1- SCRIPT 2- WHITE 3- EMB-1 4- * 5- * 6- Mrs. Petersen 7- loves 8- Mr. Petersen 9- * 10- * 11- * 12- 9990007878 13- Entered by customer: Flash App 14-  15- 71 16- 849b5629d0144e3c8293200910742e0d

I need to find a way to manipulate the string and have it formatted like :

1- SCRIPT
2- WHITE
3- EMB-1
4- *
5- *
6- Mrs. Petersen
7- loves
8- Mr. Petersen
9- *
10- *
11- *
12- 9990007878
13- Entered by customer: Flash App
14-  
15- 71
16- 849b5629d0144e3c8293200910742e0d


The character that indicates the beginning of a new field is the number + '-' sequence. There could be hyphens contained in strings like: 1- sign 1 - test line 1 2- sign 1 - test line 2 3- sign 1 - test line 3 4- sign 1 - test line 4 5- sign 1 - test line 5 6- PHONE: 1111111111

But the record separator always will be "number - space" So far the higher it goes is 16, but that may change in the future, maybe allow for that? it would be nice.

Any help would be great in how to do this using vb.net.

Many thanks.
metropiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rspahitzCommented:
How about something like this:

Dim someText As string = "1- SCRIPT 2- WHITE 3- EMB-1 4- * 5- * 6- Mrs. Petersen 7- loves 8- Mr. Petersen 9- * 10- * 11- * 12- 9990007878 13- Entered by customer: Flash App 14-  15- 71 16- 849b5629d0144e3c8293200910742e0d"
Dim items() as String = someText.split("- ")

Now each item will have the correct text (assuming that "- " does not appear in the text anywhere*).  The only problem will be that each item will have a trailing number from the previous item, which can be removed with the LastIndexOf(" ") method.

If you iterate through the items list, you can show the text something like this:

dim item as string
For idx=1 to items.length()
   item = items(idx)
   item = item.substring(0, item.lastindexof(" ")-1)
   Debug.Print item
next idx

(I don't have access to VB.Net at the moment, but this should be close)

* If this is not the case, it will take a bit more searching, maybe using regex to identify the splitting points
0
rspahitzCommented:
or how about:

Dim someText As string = "1- SCRIPT 2- WHITE 3- EMB-1 4- * 5- * 6- Mrs. Petersen 7- loves 8- Mr. Petersen 9- * 10- * 11- * 12- 9990007878 13- Entered by customer: Flash App 14-  15- 71 16- 849b5629d0144e3c8293200910742e0d"
Dim item As String
Dim idx as integer = 1
Dim posit1 As Integer = 0
Dim posit2 as Integer = 0

Do
   posit1 = someText.indexOf(idx & "- ")
   posit2 = someText.indexOf((idx + 1) & "- ")
   If posit1 < 0 or posit2 < 0 then
      Exit Do
   end If
   item = someText.substring(posit1, posit2 - posit1 - 1)
   Debug.Print item
   idx += 1
Loop
0
metropiaAuthor Commented:
The patter is going to be "number hyphen space", ther may be other hyphens within the string but will not follow that pattern i.e. "1- "

Will your example work for that?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

metropiaAuthor Commented:
The pattern is  one or more digits (0-9), a hyphen, and then a space
0
rspahitzCommented:
Try my second example.  Again, I don't have VB.Net handy at the moment (I can check it tonight) so there may be slight syntax errors, but let me know if you need help with it.
0
metropiaAuthor Commented:
I used a response.write(items)
and whilst debugging, I could see that the fields are separated correctly, but they are not written like a list format.

1- dasss
2- ddddd
3-  gggggg

they are written like a long string. I would like to add a line break after each one of the items on the list.

many thanks for your help.
0
metropiaAuthor Commented:
what about using a regex like: "\d+-\s"

can that be done easily in vb.net

i can wait, until you can help further later on.

thank you!
0
it_saigeDeveloperCommented:
You are correct, a regular expression can be used:
Imports System.Text.RegularExpressions

Module Module1
	Sub Main()
		Dim testWithCR As String = "1- SCRIPT" & vbCrLf & _
		 "2- WHITE" & vbCrLf & _
		 "3- EMB - 1" & vbCrLf & _
		 "4- *" & vbCrLf & _
		 "5- *" & vbCrLf & _
		 "6- Mrs. Petersen" & vbCrLf & _
		 "7- loves" & vbCrLf & _
		 "8- Mr. Petersen" & vbCrLf & _
		 "9- *" & vbCrLf & _
		 "10- *" & vbCrLf & _
		 "11- *" & vbCrLf & _
		 "12- 9990007878" & vbCrLf & _
		 "13- Entered by customer: Flash App" & vbCrLf & _
		 "14-  " & vbCrLf & _
		 "15- 71" & vbCrLf & _
		"16- 849b5629d0144e3c8293200910742e0d"
		Dim testFlat As String = "1- SCRIPT 2- WHITE 3- EMB-1 4- * 5- * 6- Mrs. Petersen 7- loves 8- Mr. Petersen 9- * 10- * 11- * 12- 9990007878 13- Entered by customer: Flash App 14-  15- 71 16- 849b5629d0144e3c8293200910742e0d"
		Console.WriteLine("With carriage returns")
		Dim split = Regex.Split(testWithCR, "\d+-\s", RegexOptions.IgnorePatternWhitespace)
		For Each item As String In split
			Console.WriteLine(item)
		Next
		Console.WriteLine()
		Console.WriteLine("Without carriage returns")
		Dim split2 = Regex.Split(testFlat, "\d+-\s", RegexOptions.IgnorePatternWhitespace)
		For Each item As String In split2
			Console.WriteLine(item)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
-saige-
0
rspahitzCommented:
>I used a response.write(items)
I'm not familiar with that, but many output tools have a variation to include a new-line such as, maybe response.writeln(items)

If you want to force a new line, you can simply concatenate that:

response.write(item & Environment.NewLine)
0
louisfrCommented:
If you want to include the starting number in the result, you'll need a zero-length matching pattern.
split = Regex.Split(inputstring, @"(?<!^|\d)(?=\d+-\s)")

Open in new window

That regex means: split before any sequence of digits, an hyphen and a space but not after a digit (or else it would split "10 - " before the '1' and before the '0') or the start of the input (to remove an empty string as the first entry).
0
metropiaAuthor Commented:
Hello all,

This is what I have so far:

DirectCast(e.Item.FindControl("litNote"), Literal).Text = Regex.Replace(product.Note, "(\d+-\s(?:(?!\d+-\s).)*)", "$1\n" & Environment.NewLine, RegexOptions.IgnorePatternWhitespace)

Open in new window


That gives me:
1- sign 1 - test line 1\n 2- sign 1 - test line 2\n 3- sign 1 - test line 3\n 4- sign 1 - test line 4\n 5- sign 1 - test line 5\n 6- PHONE: 1111111111\n 7- Entered by customer\n 8- COMMENTS: this is test for sign 1\n 9- 5 LINE SIGN, STYLE: 98\n 10- SKU: 10140, QTY: 1\n 11- ee69ef2aff024a458488de10f498ac10\n

Open in new window



I can see that I am separating the fields the way I want, but I am not finding so easy to add a line break after each field.

Can you share your insight and tell me what am I missing?

Many thanks.
0
metropiaAuthor Commented:
I think I need to replace: "$1\n"

With: "$1<br />"
0
rspahitzCommented:
yes, if you're building this into an HTML document, you'll need either a line break <br> or surround each item with a <p></p> pair:

"<p>$1</p>" & Environment.NewLine

The Environment.NewLine will only be useful when viewing the source of the document but will not impact the web page output.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
metropiaAuthor Commented:
thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.