This might do the trick:
<?php
$filename = "fakexample.txt";
$file = fopen($filename, "rb");
$myFile = fread($file, filesize($filename));
function get_lines($string, $myFile){
if (preg_match_all("/$string/m", $myFile, $matches))
return $matches[0];
else return array();
}
// Match lines with ; but no :
$string = '^[^;:\r\n]*;[^:\r\n]*$';
$lines = get_lines($string, $myFile);
foreach($lines as $line){
echo $line."\n";
}
?>
Additional:
Here is a breakdown of the above regex, which meets the precise original requirements stated in the question: i.e. "... isolate all lines with semicolons if they do not contain colons ..."
$re = '/ # Match line with ; but no :
^ # Anchor to start of line.
[^;:\r\n]* # Zero or more non-:, non-;
; # Match one ; (minimum required).
[^:\r\n]* # Zero or more non-:.
$ # Anchor to end of line.
/xm';
But since you insist on using the expression: '^((?!(:|\()).)*$'
, it appears that what you really want is to match are: " lines having no colons and no left parentheses." (which is what that expression does). (You probably already understand it but I always like to write expressions fully commented - can't help myself!) So here it is broken down:
$re = '/ # Match line with no colons or left parentheses.
^ # Anchor to start of line.
( # Step through line one-char at a time.
(?! # Assert that this char is NOT...
(:|\() # either a colon or a left paren.
) # End negative lookahead.
. # Safe to match next non-newline char.
)* # Step through line one-char at a time.
$ # Anchor to end of line.
/xm';
If that is what you really want, fine. But if this is the case then the above expression can be greatly simplified (and sped up) as:
$re = '/ # Match line with no colons or left parentheses.
^ # Anchor to start of line.
[^:(\r\n]* # Zero or more non-:, non-(, non-EOL.
$ # Anchor to end of line.
/xm';
And just for the sake of completeness, if what you really, really need is to match are lines "having at least one semicolon but no colons or left parentheses" Then this one will do that:
$re = '/ # Match line with ; but no : or (
^ # Anchor to start of line.
[^;:(\r\n]* # Zero or more non-:, non-;, non-(.
; # Match one ; (minimum required).
[^:(\r\n]* # Zero or more non-:, non-(.
$ # Anchor to end of line.
/xm';
When working with regex is extremely important to precisely define the requirements up front in the question. Regular expressions are a very precise language and they will only do what is asked of them.
I hope this helps!