I have a large amount of log data that I need to get some relevant information out of. The syntax is:
WORD1 some text KEYWORD some text WORD2 WORD1 some text KEYWORD some text WORD2 WORD1 some text KEYWORD some text WORD2 WORD1 some text KEYWORD some text WORD2
Would it be possible to use regex to get a certain block out of it - so that when I queried some keyword it would return WORD1 some text THIS_KEYWORD some text WORD2
PS: There could be multiple instances of one keyword, it should return all of the blocks (preg_match_all, PREG_SET_ORDER?).
As per you comment there will always be starting and finishing word for each block.
You can do the following:
WORD2
and preceded by WORD1
..
$arr = preg_split('/(?<=WORD2)\s+(?=WORD1)/',$log);
$keyword = 'KEYWORD1';
$result = preg_grep("/\b$keyword\b/",$arr);
use UNGREEDY modifier
$found = preg_match_all("/WORD1.*KEYWORD.*WORD2/U",$x,$match);
if($found) print_r($match[0]);