Re: Multiline Regex Matching
try this:
vb Code:
Dim TestRegex As String = "^([a-z]|\r\n)*$"
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
Re: Multiline Regex Matching
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.
Re: Multiline Regex Matching
Quote:
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?
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?
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.
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.
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
Re: Multiline Regex Matching
Remove that $ from the end and it will work.
Regex: ^[a-z]*
Re: Multiline Regex Matching
Your right; it did work. But why?
Re: Multiline Regex Matching
I suspect that's because the * (star) matches anything except the newline character.
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.
Re: Multiline Regex Matching
That's right... but upto what point would it go? It would breakoff at newline character, Isn't it?
Re: Multiline Regex Matching
Well, yes it wouldn't match the newline character, but isn't that what the "$" does?
Re: Multiline Regex Matching
Re: Multiline Regex Matching
Re: Multiline Regex Matching
Quote:
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.