Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use named regex groups and constants for regex strings
  • Loading branch information
ndench committed May 7, 2021
commit 1adc1889e1e27dd9b570827983a769b18e6cbf8b
10 changes: 5 additions & 5 deletions src/PhpSpreadsheet/Cell/AddressHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ public static function convertToR1C1(
?int $currentRowNumber = null,
?int $currentColumnNumber = null
): string {
$validityCheck = preg_match('/^(\$?)([A-Z]{1,3})(\$?)(\d{1,7})$/i', $address, $cellReference);
$validityCheck = preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference);

if ($validityCheck === 0) {
throw new Exception('Invalid A1-format Cell Reference');
}

$columnId = Coordinate::columnIndexFromString($cellReference[2]);
if ($cellReference[1] === '$') {
$columnId = Coordinate::columnIndexFromString($cellReference['col_ref']);
if ($cellReference['absolute_col'] === '$') {
// Column must be absolute address
$currentColumnNumber = null;
}

$rowId = (int) $cellReference[4];
if ($cellReference[3] === '$') {
$rowId = (int) $cellReference['row_ref'];
if ($cellReference['absolute_row'] === '$') {
// Row must be absolute address
$currentRowNumber = null;
}
Expand Down
6 changes: 4 additions & 2 deletions src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
abstract class Coordinate
{
public const A1_COORDINATE_REGEX = '/^(?<absolute_col>\$?)(?<col_ref>[A-Z]{1,3})(?<absolute_row>\$?)(?<row_ref>\d{1,7})$/i';

/**
* Default range variable constant.
*
Expand All @@ -29,8 +31,8 @@ abstract class Coordinate
*/
public static function coordinateFromString($pCoordinateString)
{
if (preg_match('/^([$]?[A-Z]{1,3})([$]?\\d{1,7})$/', $pCoordinateString, $matches)) {
return [$matches[1], $matches[2]];
if (preg_match(self::A1_COORDINATE_REGEX, $pCoordinateString, $matches)) {
return [$matches['absolute_col'] . $matches['col_ref'], $matches['absolute_row'] . $matches['row_ref']];
} elseif (self::coordinateIsRange($pCoordinateString)) {
throw new Exception('Cell coordinate string can not be a range of cells');
} elseif ($pCoordinateString == '') {
Expand Down