For the first part that works due to the .*
. If you want to be able to remove the second part as well, you could make both groups optional and the first one non greedy. Move the space into the second group as well.
Note that you don't have to escape the double quote and that the quantifier {1}
is superfluous so it can be omitted.
There is only a single double quote following after the first match, but to prevent possible over matching you could make that match also non greedy or use a negated character class ("[^"]+")
instead to prevent unnecessary backtracking.
(^.*?)?(\[[0-9]+:[0-9]+:[0-9]+:[0-9]+\] )?(".+?") ([0-9]+) ([0-9-]+)
Regex demo
For example
$strings = [
'141.243.1.172 [29:23:53:25] "GET /Software.html HTTP/1.0" 200 233',
'[29:23:53:25] "GET /Software.html HTTP/1.0" 200 233',
'"GET /Software.html HTTP/1.0" 200 233'
];
$pattern = '/(^.*?)?(\[[0-9]+:[0-9]+:[0-9]+:[0-9]+\] )?(".+?") ([0-9]+) ([0-9-]+)/';
foreach ($strings as $string) {
preg_match($pattern, $string, $matches);
print_r($matches);
}
Result
Array
(
[0] => 141.243.1.172 [29:23:53:25] "GET /Software.html HTTP/1.0" 200 233
[1] => 141.243.1.172
[2] => [29:23:53:25]
[3] => "GET /Software.html HTTP/1.0"
[4] => 200
[5] => 233
)
Array
(
[0] => [29:23:53:25] "GET /Software.html HTTP/1.0" 200 233
[1] =>
[2] => [29:23:53:25]
[3] => "GET /Software.html HTTP/1.0"
[4] => 200
[5] => 233
)
Array
(
[0] => "GET /Software.html HTTP/1.0" 200 233
[1] =>
[2] =>
[3] => "GET /Software.html HTTP/1.0"
[4] => 200
[5] => 233
)
Php demo