1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Regular Expressions - How to Delete Two Lines, If one contains X?

Discussion in 'General Programming Chat' started by TheWicker, May 7, 2010.

  1. TheWicker

    TheWicker Power Member

    Joined:
    May 15, 2008
    Messages:
    576
    Likes Received:
    157
    So I have a file and need to delete all instances that contain a certain string in the first line. Also Each line below the line that contains that string should also be completely deleted (no matter what it is). How do I make this happen?

    Now obviously the expression for finding that first line is:

    ^.*STRING.*$

    After that I'm stuck.

    In other words I need to delete TWO lines and the first line contains my string.
     
  2. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    Try:

    Code:
    ^.*STRING.*\r?\n.*$
    Windows uses \r\n for newlines, so make \r optional just in case. :)
     
  3. TheWicker

    TheWicker Power Member

    Joined:
    May 15, 2008
    Messages:
    576
    Likes Received:
    157
    Doen't seem to work. Let me give you an example of what I'm trying to achieve. I have some text like this one:

    Code:
    http://www.esportbike.com/forums/showthread.php?t=142594
    Superbike Toy Store<||>Shoei X 11 Kaama
    http://www.esportbike.com/forums/showthread.php?t=142484
    Superbike Toy Store<||>Sale on Arlen Ness Deep Freeze Suit
    http://forums.nba-live.com/viewtopic.php?f=76&t=51294
    [B]Entertainment Media<||>Flight of the Conchords[/B]
    I am trying to highlight this phrase "Entertainment Media<||>Flight of the Conchords". Now I want to achieve this with typing ^.*forums.nba-live.com.*\r?\n.*$ (if it worked properly). Hence selecting the line that is below the line that contains the forums.nba-live.com url.
     
  4. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    Your first post says you want to delete the matching line and the line under it, but your second post implies you're trying to match and extract the second line. Which is it?

    The regex I gave you does indeed match both lines: http://rubular.com/r/CW03tUa6qP

    If you want to capture each line within the regex, try this: http://rubular.com/r/3scdMug8as