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
LantraxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Fernando SotoRetiredCommented:
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
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

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Fernando SotoRetiredCommented:
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
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.
Fernando SotoRetiredCommented:
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
LantraxAuthor Commented:
an empty string
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.