Supported Versions: PHP 4, PHP 5, PHP 7, PHP 8
Quote regular expression characters
<?php preg_quote(string $str, ?string $delimiter = null): string
<?php foreach ($composerFiles as $composerFile) {

    $contents = $filesystem->read($composerFile);

    $mainVersionRegex = preg_quote($mainVersion, '~');

    $updated = preg_replace('~("league/flysystem": "\\^[a-zA-Z0-9\\.-]+")~ms', '"league/flysystem": "^'.$mainVersion.'"', $contents);

    $filesystem->write($composerFile, $updated);

}