|
-
Nov 30th, 2006, 10:25 AM
#1
Thread Starter
Addicted Member
RegEx Prob. Beats me!
Hi,
I just started looking at RegEx, and used one of gigemboy's to get started, which was great!
Now though I have a specific prob and I can't seem to work it out. I have a file in this format:
VB Code:
Dim Test As String = "There once was a\nboy named Freddy \nand he\nliked his big fat Teddy\n"
Dim Matches As MatchCollection = Regex.Matches(Test, "((?<!\\n)\s*[A-Za-z]+\s*)+(?=\\n)")
"\n" is a line feed character. I want to pull out all the seperate lines. With the above code I get:
There once was a
nboy named Freddy
nand he
nliked his big fat Teddy
...notice the "n" at the beginning if the 2nd and onward lines.
BTW the above is just a sample sentence for the format.
Thx for your help. RegEx looks like being really cool. Pushing the comfort zone.
-
Nov 30th, 2006, 12:47 PM
#2
Re: RegEx Prob. Beats me!
Try the below Regex sample out. One of the differences was to place each character in its own set of brackets, so it looks for a slash, and then an "n".
VB Code:
Dim Test As String = "There once was a\nboy named Freddy \nand he\nliked his big fat Teddy\n"
'using Regex
Dim Matches As System.Text.RegularExpressions.MatchCollection = _
System.Text.RegularExpressions.Regex.Matches(Test, _
"^.*?(?=[\\][n])|(?<=[\\][n]).*?(?=[\\][n])", System.Text.RegularExpressions.RegexOptions.Singleline)
For Each Match As System.Text.RegularExpressions.Match In Matches
MessageBox.Show(Match.Value)
Next
'using just string.repleace
Dim FinalString As String = Test.Replace("\n", Environment.NewLine)
MessageBox.Show(FinalString)
In this case, it might be easier to just use String.Replace, however, learning Regex can be very beneficial in the future if you ever need it...
-
Nov 30th, 2006, 04:31 PM
#3
Re: RegEx Prob. Beats me!
Or use string.split on the \n
-
Nov 30th, 2006, 08:21 PM
#4
Thread Starter
Addicted Member
Re: RegEx Prob. Beats me!
Okay, getting there. Thx.
-
Nov 30th, 2006, 08:40 PM
#5
Hyperactive Member
Re: RegEx Prob. Beats me!
This tool has helped me with regex. It is called Expresso.
http://www.ultrapico.com/Expresso.htm
Last edited by Krenshau; Nov 30th, 2006 at 08:47 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|