Results 1 to 14 of 14

Thread: [RESOLVED] Regex for ;\r\n

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Resolved [RESOLVED] Regex for ;\r\n

    Hello,

    I need to use regex to check if the line ends with ;\n\r (;vbCrLf).

    If it doesn't end that way the vbCrLf ou vbLf is to be replaced by " "

    Thank you.

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

    Re: Regex for ;\r\n

    So if there’s no \r\n, those (nonexistent) characters should be replaced with a space? How is that supposed to work?

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,694

    Re: Regex for ;\r\n

    Quote Originally Posted by .paul. View Post
    So if there’s no \r\n, those (nonexistent) characters should be replaced with a space? How is that supposed to work?
    I understood it more in the way if the ";" (semicolon) is missing to replace a vbCrLf/vbLF with a Space, that way reversing a Linewrap.
    The interesting question would be: What if the Line ends in "!\r\n" or "?\r\n" or ".\r\n" and so on?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,407

    Re: Regex for ;\r\n

    Quote Originally Posted by Paulo Lib View Post
    Hello,

    I need to use regex to check if the line ends with ;\n\r (;vbCrLf).

    If it doesn't end that way the vbCrLf ou vbLf is to be replaced by " "

    Thank you.
    So you want to look for vbCrLf or vbLf and replace these with " " only if they are not preceded by a ";" and regex writes itself:

    1. Search for vbCrLf or vbLf -- "(\r\n|\n)"
    2. For preceding ";" use negative lookbehind -- "(?<!;)(\r\n|\n)"
    3. Replace with " "

    private static readonly Regex MyPattern = new Regex(@"(?<!;)(\r\n|\n)", RegexOptions.Compiled);
    MyPattern.Replace(TextFromFile, " ");


    Edit: You can even use "(\r?\n)" for vbCrLf or vbLf search i.e. "(?<!;)(\r?\n)" for complete pattern.

    cheers,
    </wqw>

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Regex for ;\r\n

    Quote Originally Posted by .paul. View Post
    So if there’s no \r\n, those (nonexistent) characters should be replaced with a space? How is that supposed to work?
    The line is (for example):

    WyomingvbCrLf
    is a State;vbCrLf

    what I'm asking is how identify with regex is if the line ends with vbCrLf without ; to replace the vbCrLf with "" so that the output line becomes

    Wyoming is a State;vbCrLf

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,961

    Re: Regex for ;\r\n

    If you do not want to use regular expressions:
    Code:
    Private Shared Function FormatText(value As String) As String
        Dim lines = value.Split(Environment.NewLine, StringSplitOptions.None)
        Dim formattedText = New StringBuilder()
        For Each line As String In lines
            If (line.EndsWith(";")) Then
                formattedText.AppendLine(line)
            Else
                formattedText.Append($"{line} ")
            End If
        Next
        Return formattedText.ToString()
    End Function
    If you wanted to use regular expressions:
    Code:
    Private Shared Function FormatText(value As String) As String
        Dim pattern = "(?<!;)\r?\n"
        Dim replacement = " "
        Dim result = Regex.Replace(value, pattern, replacement)
        Return result
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,407

    Re: Regex for ;\r\n

    Quote Originally Posted by Paulo Lib View Post
    The line is (for example):

    WyomingvbCrLf
    is a State;vbCrLf

    what I'm asking is how identify with regex is if the line ends with vbCrLf without ; to replace the vbCrLf with "" so that the output line becomes

    Wyoming is a State;vbCrLf
    So is "(?<!;)(\r?\n)" not working?

    I did some testing here: https://regexr.com/84es1



    Seems to work.

    cheers,
    </wqw>

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,961

    Re: Regex for ;\r\n

    Quote Originally Posted by wqweto View Post
    So is "(?<!;)(\r?\n)" not working?
    I don't know why, but I didn't see your other post, but I used the same RegEx as you did. I'm curious if it isn't exactly what the OP needs.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Regex for ;\r\n

    Quote Originally Posted by wqweto View Post
    So is "(?<!(\r?\n)" not working?

    I did some testing here: https://regexr.com/84es1



    Seems to work.

    cheers,
    </wqw>
    Hi.

    Thank you for the help.

    Your approach didn't work with me either.

    It replaced all the vbCrLf with or without ; right behind the vbCrLf.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,658

    Re: Regex for ;\r\n

    Try this...

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim s As String = "Wyoming" & vbCrLf & "is a State;" & vbCrLf & "line 2"
        Dim pattern As String = "(?<!;)\r?\n"
        Dim replacement As String = " "
        Dim result As String = Regex.Replace(s, pattern, replacement)
        MsgBox(s)
        MsgBox(result)
    End Sub
    Edit: Don't forget...

    Code:
    Imports System.Text.RegularExpressions
    Last edited by .paul.; Aug 8th, 2024 at 11:16 AM.

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,407

    Re: Regex for ;\r\n

    Quote Originally Posted by Paulo Lib View Post
    Hi.

    Thank you for the help.

    Your approach didn't work with me either.

    It replaced all the vbCrLf with or without ; right behind the vbCrLf.
    Show your code which does not work. The regex above is correct and works ok in .Net dialect too.

    There must be something else we are missing here.

    cheers,
    </wqw>

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,658

    Re: Regex for ;\r\n

    Don’t forget, \n\r is not equal to \r\n
    The correct sequence of characters is \r\n i.e. crlf
    The Regex is an exact pattern matcher, in its current form it’ll match \r\n not following ; The \r is optional, but that wouldn’t work properly if your text contains \n\r or ;\n\r
    Last edited by .paul.; Aug 8th, 2024 at 06:16 PM.

  13. #13
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,407

    Re: Regex for ;\r\n

    Btw, the title of the thread is "Regex for ;\r\n" so it's safe to assume that OP must be aware the correct sequence is \r then \n and that the mentioned \n\r is wrong and not recognized as line ending by any standard.

    cheers,
    </wqw>

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Regex for ;\r\n

    Thank you for your help.

    the situation is now soved.

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