Solved

how to parse long string and add line break

Posted on 2014-11-05
14
186 Views
Last Modified: 2014-11-06
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.
0
Comment
Question by:metropia
14 Comments
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
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
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
The pattern is  one or more digits (0-9), a hyphen, and then a space
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
>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
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
I think I need to replace: "$1\n"

With: "$1<br />"
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
Comment Utility
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
 

Author Comment

by:metropia
Comment Utility
thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now