|
-
Sep 5th, 2009, 07:36 PM
#1
Thread Starter
Addicted Member
Multiline Regex Matching
I've been working with Regex and I've run into something that doesn't make sense to me; I either misunderstood how the Multiline option works, I'm not using it right, or it really just doesn't work like it's supposed to. This is a little test I made:
vb.net Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim TestString As String = "test" & vbNewLine & "sweet" Dim TestRegex As String = "^[a-z]*$" Dim ResultsString As String = String.Empty For Each StringMatch In System.Text.RegularExpressions.Regex.Matches(TestString, TestRegex, _ System.Text.RegularExpressions.RegexOptions.Multiline) ResultsString &= StringMatch Next Console.WriteLine(resultsstring) End Sub
The problem is, the only match that is found is the last line (in this case "sweet"). Why is this?
-
Sep 5th, 2009, 08:57 PM
#2
Re: Multiline Regex Matching
try this:
vb Code:
Dim TestRegex As String = "^([a-z]|\r\n)*$"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 5th, 2009, 09:08 PM
#3
Thread Starter
Addicted Member
Re: Multiline Regex Matching
If this was my string:
"oneword
twoword
threeword"
Your regex would return the following matches:
(0)"oneword
twoword
threeword"
I'd rather it return:
(0)"oneword"
(1)"twoword"
(2)"threeword"
My program has a checkbox so the user can decide whether or not they want the multiline option to be on/off; I could just split the text in the textbox at "\n" and put it into an array, then evaluate each string in the array when the user has checked the checkbox, but this would be a lot more complicated than just including/not including the multiline option.
Edit: More coherent/understandable now (hopefully) =P
Last edited by Empyreal; Sep 6th, 2009 at 12:59 PM.
-
Sep 7th, 2009, 10:13 AM
#4
Thread Starter
Addicted Member
Re: Multiline Regex Matching
-
Sep 7th, 2009, 10:23 AM
#5
Hyperactive Member
Re: Multiline Regex Matching
You can just use the Split method to separate each line in the textbox, if that's what you're trying to do.
-
Sep 7th, 2009, 10:26 AM
#6
Thread Starter
Addicted Member
Re: Multiline Regex Matching
 Originally Posted by Empyreal
I could just split the text in the textbox at "\n" and put it into an array, then evaluate each string in the array when the user has checked the checkbox, but this would be a lot more complicated than just including/not including the multiline option.
I could do that, but, to my understanding, that's exactly what the multiline option does, so why go through all that trouble?
-
Sep 7th, 2009, 10:32 AM
#7
Hyperactive Member
Re: Multiline Regex Matching
Oh yeah, but why the trouble trying to do the same with RegEx?
Also, you can grab specific lines using:
vb.net Code:
MessageBox.Show(TextBox1.Lines(2)) 'shows 3rd line
EDIT: or are you on about the multiline option in the RegEx class and not the textbox?
-
Sep 7th, 2009, 10:47 AM
#8
Thread Starter
Addicted Member
Re: Multiline Regex Matching
I'm talking about the multiline option in the regex class. Sorry, should have been more specific but I forgot all about the multiline property of the textbox =P
Anyway, I'm not trying to use regex JUST to split the text into lines; I was just using simple examples.
-
Sep 7th, 2009, 10:52 AM
#9
Hyperactive Member
Re: Multiline Regex Matching
Ah, OK. Sorry, I should have realised.
So what is the actual issue?
You may want to test your expressions with this as it has a multiline feature:
http://gskinner.com/RegExr/
Sorry I couldn't be much help.
-
Sep 7th, 2009, 11:03 AM
#10
Thread Starter
Addicted Member
Re: Multiline Regex Matching
Thanks for that site; I'll actually probably use that a lot now =P
I set up a simple test to compare that site to Vb.net:
Regex: "^[a-z]*$"
Text:
"testone
testtwo
testthree"
Options: Multiline
The site returned three matches:
(0) testone
(1) testtwo
(2) testthree
Vb.net returned one match:
(0) testthree
-
Sep 7th, 2009, 11:26 AM
#11
Re: Multiline Regex Matching
Remove that $ from the end and it will work.
Regex: ^[a-z]*
-
Sep 7th, 2009, 11:53 AM
#12
Thread Starter
Addicted Member
Re: Multiline Regex Matching
Your right; it did work. But why?
-
Sep 7th, 2009, 01:05 PM
#13
Re: Multiline Regex Matching
I suspect that's because the * (star) matches anything except the newline character.
-
Sep 7th, 2009, 01:22 PM
#14
Thread Starter
Addicted Member
Re: Multiline Regex Matching
No, that's the period (.). The star (*) matches the character(s) preceding it 0 or more times.
I.e. "[a-z]*" would match "a", "abfds", "asdlfkdjalskfjasladfgklad", and so on.
-
Sep 7th, 2009, 01:36 PM
#15
Re: Multiline Regex Matching
That's right... but upto what point would it go? It would breakoff at newline character, Isn't it?
-
Sep 7th, 2009, 03:47 PM
#16
Thread Starter
Addicted Member
Re: Multiline Regex Matching
Well, yes it wouldn't match the newline character, but isn't that what the "$" does?
-
Sep 8th, 2009, 02:21 AM
#17
Re: Multiline Regex Matching
-
Sep 8th, 2009, 02:25 AM
#18
Re: Multiline Regex Matching
-
Sep 8th, 2009, 05:59 AM
#19
Thread Starter
Addicted Member
Re: Multiline Regex Matching
 Originally Posted by msdn
The match must occur at the end of the string or before \n at the end of the line or string.
http://www.regular-expressions.info/anchors.html
Above is another site with a really good explanation of anchors. Look at the "Using ^ and $ as Start of Line and End of Line Anchors" section.
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
|