Results 1 to 8 of 8

Thread: Regular Expressions [resolved]

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Regular Expressions [resolved]

    I am trying to learn regular expressions and I have the basics down, but it is a pain. I am trying to parse the code that forums like this use: ie [b ]bold stuff[/b ], but I am having trouble if they go over multiple lines.
    Code:
    Dim linksExpression As Regex
    Dim strPattern As String = "\[B ](.+?)\[/B ]"
    linksExpression = New Regex(strPattern, RegexOptions.Multiline Or RegexOptions.IgnoreCase)
    strTemp = linksExpression.Replace(strTemp, "<b>$1</b>")
    this works fine if I have it all on one line like [b ]bold stuff[/b ], but if I do
    [b ]bold
    stuff[/b ], it doesn't work.
    Last edited by blindlizard; Sep 23rd, 2003 at 02:51 PM.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try removing the RegexOptions.Multiline option. That will process each line individually instead of all of the string as one.

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Tried that, same thing happens.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Post some sample text to test on and i'll take a gander at it.

  5. #5

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Code:
    Dim linksExpression As Regex
    Dim strPattern As String = "\[B ](.+?)\[/B ]"
    strTemp = "[B ]bold" & vbCrLf & "this[/B ]"
    linksExpression = New Regex(strPattern, RegexOptions.IgnoreCase)
    strTemp = linksExpression.Replace(strTemp, "<b>$1</b>")
    'strTemp returns what was sent in
    
    Dim linksExpression As Regex
    Dim strPattern As String = "\[B ](.+?)\[/B ]"
    strTemp = "[B ]bold" & vbCrLf & "this[/B ]"
    linksExpression = New Regex(strPattern, RegexOptions.IgnoreCase)
    strTemp = linksExpression.Replace(strTemp, "<b>$1</b>")
    'strTemp returns <b>bold this</b>
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  6. #6

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    I am thinking that I may have to remove the vbCrlf and put in something like <br> and then put it back to VbCrlf at the end.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You have to use the SingleLine option:
    VB Code:
    1. Dim strPattern As String = "\[B ](.*)\[/B ]"
    2.         Dim linksExpression As New Regex(strPattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    3.         Dim strTemp As String = "[B ]bold" & vbCrLf & "this[/B ]"
    4.         strTemp = linksExpression.Replace(strTemp, "<b>$1</b>")
    5.         MsgBox(strTemp)

  8. #8

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Thanks man! I would never have found that. Documentation on the web is crap for regular expressions in .Net
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

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