?
Solved

Deleteing a line from a string...

Posted on 2004-11-09
12
Medium Priority
?
215 Views
Last Modified: 2010-05-18
I'm hoping someone might be able to help me with this problem.   What I want to do is use a Regual expression to find matches to a pattern in a rather long string in memory.

However, once that match is found, I wish to DELETE the line the match was found on from the string.

For example
if my string=
"
my cat is in the hat
my bird is in a cage
my fish is looking at the cat
my sheep baa
my bird can talk"

and the pattern is 'bird'
then I wish to delete the two lines containing the 'bird' (because.. you know it's the word)  and return the rest of the string..

example of string AFTER pattern matches:
"
my cat is in the hat
my fish is looking at the cat
my sheep baa
"

How would one go about doing this?

TIA!!!
0
Comment
Question by:pogowolf
  • 5
  • 5
  • 2
12 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 12538056
do you separate the lines with a vbCrLf?
0
 

Author Comment

by:pogowolf
ID: 12538748
Yes.  a NewLine and Carrage return.
0
 
LVL 3

Accepted Solution

by:
eekj earned 750 total points
ID: 12539473
Imports System.Text.RegularExpressions
...

 Dim SourceStr, str As String
 Dim SourceStrArray As String()
 Dim OutputStr As String = ""

 SourceStrArray = str.Split(vbCrLf.ToCharArray())
 For Each str In SourceStrArray
     If Not Regex.Match(str, "[.]*[bB][iI][rR][dD][.]*").Success Then
         OutputStr = OutputStr + str + vbCrLf
     End If
 Next
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.

 
LVL 3

Expert Comment

by:eekj
ID: 12539484
sorry,
 SourceStrArray = SourceStr.Split(vbCrLf.ToCharArray())
'DELETE: SourceStrArray = str.Split(vbCrLf.ToCharArray())
0
 

Author Comment

by:pogowolf
ID: 12544090
Thanks eekj,
   I'll check out the code in a moment, I do believe that's what I'm looking for..

Makes me wonder though.. is pattern Matching on a WHOLE string (like 600+ lines long) faster then
searching line by line?
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12546279
eekj gave you the solution i was thinking in...
0
 

Author Comment

by:pogowolf
ID: 12546661
I'll accept the answer, though it's not 100% what i was looking for it does work.
0
 
LVL 3

Expert Comment

by:eekj
ID: 12548215
lame
0
 

Author Comment

by:pogowolf
ID: 12548408
No, it's not 100%  what I was looking for.  After a little more research and asking around I was able to create a solution:


Dim BlackList as NEW ArrayList
Dim Footer as String = "\r\n"
BlackList.Add(Pattern & Footer)
   
Dim options As RegexOptions = RegexOptions.None
Dim regex As Regex = new Regex(BlackList(0), options)
    Dim input As string = SourceString
    Dim replacement As string = ""
    OutputStr = regex.Replace(input, replacement)
End Sub


The code above speeds up your routine about 25x.
0
 
LVL 3

Expert Comment

by:eekj
ID: 12549736
>>Dim Footer as String = "\r\n"
you need to escape the "\r\n" as "\\r\\n"

the easiest way in that case is just:
OutStr = Regex.Replace(InStr, "[.]*[bB][iI][rR][dD][.]*\\r\\n", "")
0
 

Author Comment

by:pogowolf
ID: 12554676
If I might ask, Why is the escape needed?  I thought the \r was already escaped.  wouldn't "\\R" look for a '\' and then an 'r' in the pattern?

0
 
LVL 3

Expert Comment

by:eekj
ID: 12557756
The regular expression \\ matches a single backslash.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question