jaybo Posted January 5, 2023 Share Posted January 5, 2023 (edited) 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 January 5, 2023 by requinix please use the Code <> button when posting stuff like that Quote Link to comment https://forums.phpfreaks.com/topic/315763-regex-to-start-at-one-line-and-end-after-an-ip-address-on-another-line-of-an-array/ Share on other sites More sharing options...
requinix Posted January 5, 2023 Share Posted January 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315763-regex-to-start-at-one-line-and-end-after-an-ip-address-on-another-line-of-an-array/#findComment-1604250 Share on other sites More sharing options...
jaybo Posted January 6, 2023 Author Share Posted January 6, 2023 Ok great - I'll work on that and get back to the forum! Quote Link to comment https://forums.phpfreaks.com/topic/315763-regex-to-start-at-one-line-and-end-after-an-ip-address-on-another-line-of-an-array/#findComment-1604367 Share on other sites More sharing options...
jaybo Posted January 26, 2023 Author Share Posted January 26, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/315763-regex-to-start-at-one-line-and-end-after-an-ip-address-on-another-line-of-an-array/#findComment-1605073 Share on other sites More sharing options...
requinix Posted January 26, 2023 Share Posted January 26, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315763-regex-to-start-at-one-line-and-end-after-an-ip-address-on-another-line-of-an-array/#findComment-1605104 Share on other sites More sharing options...
benanamen Posted January 27, 2023 Share Posted January 27, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315763-regex-to-start-at-one-line-and-end-after-an-ip-address-on-another-line-of-an-array/#findComment-1605115 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.