Jump to content

Regex to start at one line and end after an IP address on another line of an array


Recommended Posts

Hi,

I am outputting an array and splitting up the result sets using regular expression... I need to select the range of data that will select the data from gpononu on one line up until the end of the first IP address on another line.

example array:

  [0] => bridge show
    [1] =>
    [2] =>                                      
    [3] =>          Orig
    [4] =>
    [5] => Type     VLAN/SLAN  VLAN/SLAN      Physical                  Bridge                              St  Table Data
    [6] =>
    [7] => ---------------------------------------------------------------------------------------------------------------------
    [8] =>
    [9] => dwn-p2p             Tagged  222    1/1/1/1/gpononu           1-1-1-257-gponport-222/bridge       UP  D 00:02:71:db:bb:eb
    [10] =>
    [11] =>                                                                                                      D 216.19.250.121
    [12] =>
    [13] => dwn-p2p             Tagged  222    1/1/1/2/gpononu           1-1-1-258-gponport-222/bridge       UP  D 00:02:71:db:bb:df
    [14] =>
    [15] =>                                                                                                      D 216.19.250.138

 

I know that I can select the line with gpononu by using: gpononu

and I know I can find the IP's using:  \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

 

But not sure how to put that together to start and end the pattern match.

 

 

Edited by requinix
please use the Code <> button when posting stuff like that

Do yourself a favor and match the entire set of data, then keep from it the part you want.

Also do some preprocessing: it looks like you can skip lines 0-8 every time because those will be occupied by the header, then there are pairs of output on lines 9/11 and 13/15 (skipping the ones in between). If you merge the paired lines together you get

dwn-p2p             Tagged  222    1/1/1/1/gpononu           1-1-1-257-gponport-222/bridge       UP  D 00:02:71:db:bb:eb                                                                                                     D 216.19.250.121
dwn-p2p             Tagged  222    1/1/1/2/gpononu           1-1-1-258-gponport-222/bridge       UP  D 00:02:71:db:bb:df                                                                                                     D 216.19.250.138

which is going to be much easier to use with a regular expressions for getting the parts in between.

  • 3 weeks later...

Update -

So I filtered the array using array_filter() to remove the empty array sets throughout the results set.

Then I found that I could select the code I needed by testing on regexr.com with the expression 'gpononu.*\n.*?$' in a preg_match_all() function. On regexr.com the flags used are /gm but when I try to run in my code I am unable to get anything to print to the browser.

// $resultOutput is the ARRAY FROM RESULT

//remove empty array sets
$resultOutput1 = array_filter($resultOutput);


    $resultOutput2 = preg_match_all('gpononu.*\n.*?$', $resultOutput1);

    echo '<pre>';
    echo "\n------------------------>>>>>>>\n";
    print_r($resultOutput2);
    echo "\n------------------------>>>>>>>\n";
    echo '</pre>';

I have tried to add /gm to the regex but it still doesn't work. Any ideas?

The preg* functions require delimiters around the regex.

$resultOutput2 = preg_match_all('/gpononu.*\n.*?$/', $resultOutput1);

Flags like /g (which doesn't exist in PHP because that's what preg_match_all does) and /m (which you'll want here) would be added after them.

You can use a combination of the preg_grep() function and regular expressions to select the range of data that you want. The preg_grep() function returns all elements of an input array that match a certain regular expression pattern.

Here's an example of how you can use it to select the range of data that you want:

$start = preg_grep("/gpononu/", $array);
$start_key = key($start);
$end = preg_grep("/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/", $array);
$end_key = key($end);
$result = array_slice($array, $start_key, $end_key - $start_key + 1);
print_r($result);

This will give you an array containing the lines from the input array that match the pattern "gpononu" (line 9 and 13) until the first line that matches the pattern for IP addresses (line 11 and 15) You can use array_slice with 3rd parameter as $end_key - $start_key + 1 , this will give you desired result.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.