-
Aug 7th, 2024, 06:27 AM
#1
Thread Starter
Junior Member
[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.
-
Aug 7th, 2024, 06:41 AM
#2
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2024, 06:49 AM
#3
Re: Regex for ;\r\n
Originally Posted by .paul.
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
-
Aug 7th, 2024, 07:30 AM
#4
Re: Regex for ;\r\n
Originally Posted by Paulo Lib
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>
-
Aug 7th, 2024, 08:16 AM
#5
Thread Starter
Junior Member
Re: Regex for ;\r\n
Originally Posted by .paul.
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
-
Aug 7th, 2024, 08:55 AM
#6
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
-
Aug 7th, 2024, 09:00 AM
#7
Re: Regex for ;\r\n
Originally Posted by Paulo Lib
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>
-
Aug 7th, 2024, 09:23 AM
#8
Re: Regex for ;\r\n
Originally Posted by wqweto
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.
-
Aug 8th, 2024, 08:40 AM
#9
Thread Starter
Junior Member
Re: Regex for ;\r\n
Originally Posted by wqweto
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.
-
Aug 8th, 2024, 09:30 AM
#10
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 8th, 2024, 09:44 AM
#11
Re: Regex for ;\r\n
Originally Posted by Paulo Lib
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>
-
Aug 8th, 2024, 06:11 PM
#12
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 9th, 2024, 05:17 AM
#13
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>
-
Aug 14th, 2024, 06:32 AM
#14
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|