Results 1 to 19 of 19

Thread: Multiline Regex Matching

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim TestString As String = "test" & vbNewLine & "sweet"
    3.     Dim TestRegex As String = "^[a-z]*$"
    4.     Dim ResultsString As String = String.Empty
    5.     For Each StringMatch In System.Text.RegularExpressions.Regex.Matches(TestString, TestRegex, _
    6.                                                                              System.Text.RegularExpressions.RegexOptions.Multiline)
    7.         ResultsString &= StringMatch
    8.     Next
    9.  
    10.     Console.WriteLine(resultsstring)
    11. End Sub

    The problem is, the only match that is found is the last line (in this case "sweet"). Why is this?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Multiline Regex Matching

    try this:

    vb Code:
    1. Dim TestRegex As String = "^([a-z]|\r\n)*$"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    Re: Multiline Regex Matching

    Bump

  5. #5
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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?

  7. #7
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    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:
    1. 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?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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.

  9. #9
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    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.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Multiline Regex Matching

    Remove that $ from the end and it will work.

    Regex: ^[a-z]*
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    Re: Multiline Regex Matching

    Your right; it did work. But why?

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Multiline Regex Matching

    I suspect that's because the * (star) matches anything except the newline character.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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.

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Multiline Regex Matching

    That's right... but upto what point would it go? It would breakoff at newline character, Isn't it?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    Re: Multiline Regex Matching

    Well, yes it wouldn't match the newline character, but isn't that what the "$" does?

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Multiline Regex Matching

    $ = match end of string
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width