You are here

function RegexCounter::countGroupsIgnoreHellternations in Bibliography Module 7.2

Returns how many groups (numbered or named) there are in the given $pattern, ignoring hellternations (?|...|...)

Parameters

string $pattern:

array $namedGroups:

array $numberedGroups:

Return value

integer

1 call to RegexCounter::countGroupsIgnoreHellternations()
RegexCounter::countGroups in lib/msrc-authortool/src/Nametools/RegexCounter.php
Returns how many groups (numbered or named) there are in the given $pattern

File

lib/msrc-authortool/src/Nametools/RegexCounter.php, line 76

Class

RegexCounter
Regexcounter is a class that counts the expected number of matches for a given REGEX pattern

Namespace

Nametools

Code

function countGroupsIgnoreHellternations(&$pattern, &$namedGroups, &$numberedGroups) {
  $findExplicitelyEscaped = '/\\\\./';
  $pattern = preg_replace($findExplicitelyEscaped, '%', $pattern);
  $findImplicitelyEscaped = '/\\[[^\\]]*\\]/';
  $pattern = preg_replace($findImplicitelyEscaped, '%', $pattern);
  $findNumberedGroups = '/\\((?!\\?)/';
  $numberedGroups_count = preg_match_all($findNumberedGroups, $pattern, $numberedGroups, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  $findConditions = '/\\(\\?\\(/';
  $conditions_count = preg_match_all($findConditions, $pattern, $dummy);
  $numberedGroups_count -= $conditions_count;
  $find_namedGroups = '/\\(\\?P?(?:(?:<([^>]+)>)|(?:\'([^\']+)\'))/';
  $namedGroups_count = preg_match_all($find_namedGroups, $pattern, $namedGroups, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  $numberedGroups_count += $namedGroups_count;

  //named groups also add as many numbered groups
  $dupnames = array();
  foreach ($namedGroups as $named_group) {
    $name = $named_group[1][0];
    if (isset($dupnames[$name])) {
      $dupnames[$name] += 1;
    }
    else {
      $dupnames[$name] = 0;
    }
  }
  $dupnames_count = array_sum($dupnames);
  $namedGroups_count -= $dupnames_count;

  //duplicate names are added only once
  $result = $numberedGroups_count + $namedGroups_count;
  return $result;
}