#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Grab N line Regex


    Hello,

    First I want to say Hi to everyone and thanks in advance .

    Example:
    Anything
    Anything
    FIXED WORD 1
    Anything
    Anything
    Anything
    FIXE WORD 2

    Regex=FIXED WORD 1

    Todo: I want to grab N lines after the Regex. For the example, let's say I want to grab the 2 lines after FIXED WORD 1.
    I'm looking for a regex to say "SELECT 2 LINE AFTER FIXED WORD 1".

    Text between fixed word could really be anything, I just need to keep the 2 first lines.

    I search an expression or a keyword to qualify a "line". I will use this regex with Java.

    Need help for that,

    Many thanks.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,004
    Rep Power
    9398
    Code:
    /
      ^FIXED WORD 1$\s+  # find a line containing just this fixed word. then skip the newline
      (.*(?:\s+.*){1})   # grab a line, then grab multiple sets of newline+line
    /mx
    Code:
    "^FIXED WORD 1$\s+(.*(?:\s+.*){1})" with the Multiline option
    The {1} is one less than however many lines you want because the first line is captured in the first .*. To get two lines you use {1} and to grab 9 lines you'd use {8}.

IMN logo majestic logo threadwatch logo seochat tools logo