• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

regular expression ignore

i have a string that has many carrage returns in it. I want to capture all the lines although i want to strip out the carrage returns. I have an identifier and an ending point such as 'Comments' for a starting point and 'Email' as an ending point.

Example:

Comments: This is an example of
what i mean and i can't
figure out how to get the carrage
returns out of this string.
Email: me@there.com



Please Help
0
Lantrax
Asked:
Lantrax
  • 4
  • 3
1 Solution
 
sr101880Commented:
To remove the carriage returns and replace them with a space:

        Dim test As String
        'test = "The Cat" & Chr(13) & "went too" & Chr(13) & "the vet."  '<------------- This would be an example of your text.
       

        test = test.Replace(Chr(10).ToString, " ")

you can use the trim functions after to remove any unwanted words.  Let me know if you have any questions.
0
 
Fernando SotoCommented:
Hi Lantrax;

Here is a solution using Regular Expression.

Imports System.Text.RegularExpressions

        ' The next two statements are for test data
        Dim cr As String = ControlChars.Cr
        Dim input As String = "Comments: This is an example of" & cr & _
            "what i mean and i can't" & cr & _
            "figure out how to get the carrage" & cr & _
            "returns out of this string." & cr & _
            "Email: me@there.com" & cr

        ' Replace the Carrage Return with a space
        Dim output As String = Regex.Replace(input, "\r", " ")

        ' If the input has a Line Feed character and not a Carrage Return
        ' then this line will Replace the Line feed with a space
        Dim output As String = Regex.Replace(input, "\n", " ")

        ' If the input has a Carrage Return/Line Feed characters and not a Carrage Return
        ' then this line will Replace the Carrage Return/Line feed with a space
        Dim output As String = Regex.Replace(input, "\r\n", " ")


Fernando
0
 
LantraxAuthor Commented:
Fernando,

You are close to what i am looking for. Although the data is in a text file. I actually grab the data between 'Comments:' and 'Email:'.  So what I was looking for a RegEx that would allow me to grab the data and do a replace at the same time. Without having to set variables as you are in the example above.

Thanks.
L

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Fernando SotoCommented:
Hi Lantrax;

The question you posted was to remove all carriage returns from a string, answered above.

Can you please state the new question.

You want to capture the following block of text?
    Comments: This is an example of
    what i mean and i can't
    figure out how to get the carriage
    returns out of this string.
   Email: me@there.com

You want to replace the following text from the above block of text?
    This is an example of
    what i mean and i can't
    figure out how to get the carriage
    returns out of this string.

What do you want to replace it with?

Will any of the text that you want to replace appears in the following character set?
    . $ ^ { [ ( | ) * + ? \

Fernando
0
 
LantraxAuthor Commented:
Sorry about that. I want to replace any type of carrage return or line feed. I want to replace the line feeds with a space.

Thanks Fernando.
0
 
Fernando SotoCommented:
So you want to replace any carriage return or line feed in the text.

You want to replace the line feed with a space character.

What do you want to replace the carriage return with
    a) With another space character?
    b) With no character the empty string, "".?

Fernando
0
 
LantraxAuthor Commented:
an empty string
0
 
Fernando SotoCommented:
Hi Lantrax;

The following code will set up the Regex pattern and options open a file which you will need to change the path and name to fit your needs and read that file into a variable called input. Then the code executes the Regex method to replace all carriage returns with an empty string and all line feeds with a space character. After the execution of the Regex Replace method the input string is replaced with the new version.

Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text

        Dim pattern As String = "^Comments:(?:.*?(\r|\n).*?)+?Email:.*?$"
        Dim reOptions As RegexOptions = RegexOptions.Multiline Or RegexOptions.Singleline Or _
            RegexOptions.IgnorePatternWhitespace
        Dim sr As New StreamReader("C:\Temp\ReplaceText.txt")
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        input = Regex.Replace(input, pattern, AddressOf ReplaceCrOrLf, reOptions)


    Private Function ReplaceCrOrLf(ByVal m As Match) As String

        Dim str As New StringBuilder(m.Value())

        str.Replace(ControlChars.Cr, "")
        str.Replace(ControlChars.Lf, " ")
        Return str.ToString()

    End Function


Fernando
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now