Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gm

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"\s+\w+\s+implements\s+(.*)\s+\{" test_str = ("<?php \n" "namespace Zend\\Db\\Table\\Row;\n" "/**\n" " * Zend Framework\n" " *\n" " * LICENSE\n" " *\n" " * This source file is subject to the new BSD license that is bundled\n" " * with this package in the file LICENSE.txt.\n" " * It is also available through the world-wide-web at this URL:\n" " * http://framework.zend.com/license/new-bsd\n" " * If you did not receive a copy of the license and are unable to\n" " * obtain it through the world-wide-web, please send an email\n" " * to license@zend.com so we can send you a copy immediately.\n" " *\n" " * @category Zend\n" " * @package Zend_Db\n" " * @subpackage Table\n" " * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)\n" " * @license http://framework.zend.com/license/new-bsd New BSD License\n" " * @version $Id$\n" " */\n\n" "/**\n" " * @see Zend_Db\n" " */\n" "require_once 'Zend/Db.php';\n\n" "/**\n" " * @category Zend\n" " * @package Zend_Db\n" " * @subpackage Table\n" " * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)\n" " * @license http://framework.zend.com/license/new-bsd New BSD License\n" " */\n" "abstract class RowAbstract implements \\ArrayAccess, IteratorAggregate\n" "{\n\n" " /**\n" " * The data for each column in the row (column_name => value).\n" " * The keys must match the physical names of columns in the\n" " * table for which this row is defined.\n" " *\n" " * @var array\n" " */\n" " protected $_data = array();\n\n" " /**\n" " * This is set to a copy of $_data when the data is fetched from\n" " * a database, specified as a new tuple in the constructor, or\n" " * when dirty data is posted to the database with save().\n" " *\n" " * @var array\n" " */\n" " protected $_cleanData = array();\n\n" " /**\n" " * Tracks columns where data has been updated. Allows more specific insert and\n" " * update operations.\n" " *\n" " * @var array\n" " */\n" " protected $_modifiedFields = array();\n\n" " /**\n" " * \\Zend\\Db\\Table\\TableAbstract parent class or instance.\n" " *\n" " * @var \\Zend\\Db\\Table\\TableAbstract\n" " */\n" " protected $_table = null;\n\n" " /**\n" " * Connected is true if we have a reference to a live\n" " * \\Zend\\Db\\Table\\TableAbstract object.\n" " * This is false after the Rowset has been deserialized.\n" " *\n" " * @var boolean\n" " */\n" " protected $_connected = true;\n\n" " /**\n" " * A row is marked read only if it contains columns that are not physically represented within\n" " * the database schema (e.g. evaluated columns/Zend_Db_Expr columns). This can also be passed\n" " * as a run-time config options as a means of protecting row data.\n" " *\n" " * @var boolean\n" " */\n" " protected $_readOnly = false;\n\n" " /**\n" " * Name of the class of the \\Zend\\Db\\Table\\TableAbstract object.\n" " *\n" " * @var string\n" " */\n" " protected $_tableClass = null;\n\n" " /**\n" " * Primary row key(s).\n" " *\n" " * @var array\n" " */\n" " protected $_primary;\n\n" " /**\n" " * Constructor.\n" " *\n" " * Supported params for $config are:-\n" " * - table = class name or object of type \\Zend\\Db\\Table\\TableAbstract\n" " * - data = values of columns in this row.\n" " *\n" " * @param array $config OPTIONAL Array of user-specified config options.\n" " * @return void\n" " * @throws Zend_Db_Table_Row_Exception\n" " */\n" " public function __construct(array $config = array())\n" " {\n" " if (isset($config['table']) && $config['table'] instanceof \\Zend\\Db\\Table\\TableAbstract) {\n" " $this->_table = $config['table'];\n" " $this->_tableClass = get_class($this->_table);\n" " } elseif ($this->_tableClass !== null) {\n" " $this->_table = $this->_getTableFromString($this->_tableClass);\n" " }\n\n" " if (isset($config['data'])) {\n" " if (!is_array($config['data'])) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('Data must be an array');\n" " }\n" " $this->_data = $config['data'];\n" " }\n" " if (isset($config['stored']) && $config['stored'] === true) {\n" " $this->_cleanData = $this->_data;\n" " }\n\n" " if (isset($config['readOnly']) && $config['readOnly'] === true) {\n" " $this->setReadOnly(true);\n" " }\n\n" " // Retrieve primary keys from table schema\n" " if (($table = $this->_getTable())) {\n" " $info = $table->info();\n" " $this->_primary = (array) $info['primary'];\n" " }\n\n" " $this->init();\n" " }\n\n" " /**\n" " * Transform a column name from the user-specified form\n" " * to the physical form used in the database.\n" " * You can override this method in a custom Row class\n" " * to implement column name mappings, for example inflection.\n" " *\n" " * @param string $columnName Column name given.\n" " * @return string The column name after transformation applied (none by default).\n" " * @throws Zend_Db_Table_Row_Exception if the $columnName is not a string.\n" " */\n" " protected function _transformColumn($columnName)\n" " {\n" " if (!is_string($columnName)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('Specified column is not a string');\n" " }\n" " // Perform no transformation by default\n" " return $columnName;\n" " }\n\n" " /**\n" " * Retrieve row field value\n" " *\n" " * @param string $columnName The user-specified column name.\n" " * @return string The corresponding column value.\n" " * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row.\n" " */\n" " public function __get($columnName)\n" " {\n" " $columnName = $this->_transformColumn($columnName);\n" " if (!array_key_exists($columnName, $this->_data)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Specified column \\\"$columnName\\\" is not in the row\");\n" " }\n" " return $this->_data[$columnName];\n" " }\n\n" " /**\n" " * Set row field value\n" " *\n" " * @param string $columnName The column key.\n" " * @param mixed $value The value for the property.\n" " * @return void\n" " * @throws Zend_Db_Table_Row_Exception\n" " */\n" " public function __set($columnName, $value)\n" " {\n" " $columnName = $this->_transformColumn($columnName);\n" " if (!array_key_exists($columnName, $this->_data)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Specified column \\\"$columnName\\\" is not in the row\");\n" " }\n" " $this->_data[$columnName] = $value;\n" " $this->_modifiedFields[$columnName] = true;\n" " }\n\n" " /**\n" " * Unset row field value\n" " *\n" " * @param string $columnName The column key.\n" " * @return Zend_Db_Table_Row_Abstract\n" " * @throws Zend_Db_Table_Row_Exception\n" " */\n" " public function __unset($columnName)\n" " {\n" " $columnName = $this->_transformColumn($columnName);\n" " if (!array_key_exists($columnName, $this->_data)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Specified column \\\"$columnName\\\" is not in the row\");\n" " }\n" " if ($this->isConnected() && in_array($columnName, $this->_table->info('primary'))) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Specified column \\\"$columnName\\\" is a primary key and should not be unset\");\n" " }\n" " unset($this->_data[$columnName]);\n" " return $this;\n" " }\n\n" " /**\n" " * Test existence of row field\n" " *\n" " * @param string $columnName The column key.\n" " * @return boolean\n" " */\n" " public function __isset($columnName)\n" " {\n" " $columnName = $this->_transformColumn($columnName);\n" " return array_key_exists($columnName, $this->_data);\n" " }\n\n" " /**\n" " * Store table, primary key and data in serialized object\n" " *\n" " * @return array\n" " */\n" " public function __sleep()\n" " {\n" " return array('_tableClass', '_primary', '_data', '_cleanData', '_readOnly' ,'_modifiedFields');\n" " }\n\n" " /**\n" " * Setup to do on wakeup.\n" " * A de-serialized Row should not be assumed to have access to a live\n" " * database connection, so set _connected = false.\n" " *\n" " * @return void\n" " */\n" " public function __wakeup()\n" " {\n" " $this->_connected = false;\n" " }\n\n" " /**\n" " * Proxy to __isset\n" " * Required by the ArrayAccess implementation\n" " *\n" " * @param string $offset\n" " * @return boolean\n" " */\n" " public function offsetExists($offset)\n" " {\n" " return $this->__isset($offset);\n" " }\n\n" " /**\n" " * Proxy to __get\n" " * Required by the ArrayAccess implementation\n" " *\n" " * @param string $offset\n" " * @return string\n" " */\n" " public function offsetGet($offset)\n" " {\n" " return $this->__get($offset);\n" " }\n\n" " /**\n" " * Proxy to __set\n" " * Required by the ArrayAccess implementation\n" " *\n" " * @param string $offset\n" " * @param mixed $value\n" " */\n" " public function offsetSet($offset, $value)\n" " {\n" " $this->__set($offset, $value);\n" " }\n\n" " /**\n" " * Proxy to __unset\n" " * Required by the ArrayAccess implementation\n" " *\n" " * @param string $offset\n" " */\n" " public function offsetUnset($offset)\n" " {\n" " return $this->__unset($offset);\n" " }\n\n" " /**\n" " * Initialize object\n" " *\n" " * Called from {@link __construct()} as final step of object instantiation.\n" " *\n" " * @return void\n" " */\n" " public function init()\n" " {\n" " }\n\n" " /**\n" " * Returns the table object, or null if this is disconnected row\n" " *\n" " * @return \\Zend\\Db\\Table\\TableAbstract|null\n" " */\n" " public function getTable()\n" " {\n" " return $this->_table;\n" " }\n\n" " /**\n" " * Set the table object, to re-establish a live connection\n" " * to the database for a Row that has been de-serialized.\n" " *\n" " * @param \\Zend\\Db\\Table\\TableAbstract $table\n" " * @return boolean\n" " * @throws Zend_Db_Table_Row_Exception\n" " */\n" " public function setTable(\\Zend\\Db\\Table\\TableAbstract $table = null)\n" " {\n" " if ($table == null) {\n" " $this->_table = null;\n" " $this->_connected = false;\n" " return false;\n" " }\n\n" " $tableClass = get_class($table);\n" " if (! $table instanceof $this->_tableClass) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"The specified Table is of class $tableClass, expecting class to be instance of $this->_tableClass\");\n" " }\n\n" " $this->_table = $table;\n" " $this->_tableClass = $tableClass;\n\n" " $info = $this->_table->info();\n\n" " if ($info['cols'] != array_keys($this->_data)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('The specified Table does not have the same columns as the Row');\n" " }\n\n" " if (! array_intersect((array) $this->_primary, $info['primary']) == (array) $this->_primary) {\n\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"The specified Table '$tableClass' does not have the same primary key as the Row\");\n" " }\n\n" " $this->_connected = true;\n" " return true;\n" " }\n\n" " /**\n" " * Query the class name of the Table object for which this\n" " * Row was created.\n" " *\n" " * @return string\n" " */\n" " public function getTableClass()\n" " {\n" " return $this->_tableClass;\n" " }\n\n" " /**\n" " * Test the connected status of the row.\n" " *\n" " * @return boolean\n" " */\n" " public function isConnected()\n" " {\n" " return $this->_connected;\n" " }\n\n" " /**\n" " * Test the read-only status of the row.\n" " *\n" " * @return boolean\n" " */\n" " public function isReadOnly()\n" " {\n" " return $this->_readOnly;\n" " }\n\n" " /**\n" " * Set the read-only status of the row.\n" " *\n" " * @param boolean $flag\n" " * @return boolean\n" " */\n" " public function setReadOnly($flag)\n" " {\n" " $this->_readOnly = (bool) $flag;\n" " }\n\n" " /**\n" " * Returns an instance of the parent table's \\Zend\\Db\\Table\\Select object.\n" " *\n" " * @return \\Zend\\Db\\Table\\Select\n" " */\n" " public function select()\n" " {\n" " return $this->getTable()->select();\n" " }\n\n" " /**\n" " * Saves the properties to the database.\n" " *\n" " * This performs an intelligent insert/update, and reloads the\n" " * properties with fresh data from the table on success.\n" " *\n" " * @return mixed The primary key value(s), as an associative array if the\n" " * key is compound, or a scalar if the key is single-column.\n" " */\n" " public function save()\n" " {\n" " /**\n" " * If the _cleanData array is empty,\n" " * this is an INSERT of a new row.\n" " * Otherwise it is an UPDATE.\n" " */\n" " if (empty($this->_cleanData)) {\n" " return $this->_doInsert();\n" " } else {\n" " return $this->_doUpdate();\n" " }\n" " }\n\n" " /**\n" " * @return mixed The primary key value(s), as an associative array if the\n" " * key is compound, or a scalar if the key is single-column.\n" " */\n" " protected function _doInsert()\n" " {\n" " /**\n" " * A read-only row cannot be saved.\n" " */\n" " if ($this->_readOnly === true) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('This row has been marked read-only');\n" " }\n\n" " /**\n" " * Run pre-INSERT logic\n" " */\n" " $this->_insert();\n\n" " /**\n" " * Execute the INSERT (this may throw an exception)\n" " */\n" " $data = array_intersect_key($this->_data, $this->_modifiedFields);\n" " $primaryKey = $this->_getTable()->insert($data);\n\n" " /**\n" " * Normalize the result to an array indexed by primary key column(s).\n" " * The table insert() method may return a scalar.\n" " */\n" " if (is_array($primaryKey)) {\n" " $newPrimaryKey = $primaryKey;\n" " } else {\n" " //ZF-6167 Use tempPrimaryKey temporary to avoid that zend encoding fails.\n" " $tempPrimaryKey = (array) $this->_primary;\n" " $newPrimaryKey = array(current($tempPrimaryKey) => $primaryKey);\n" " }\n\n" " /**\n" " * Save the new primary key value in _data. The primary key may have\n" " * been generated by a sequence or auto-increment mechanism, and this\n" " * merge should be done before the _postInsert() method is run, so the\n" " * new values are available for logging, etc.\n" " */\n" " $this->_data = array_merge($this->_data, $newPrimaryKey);\n\n" " /**\n" " * Run post-INSERT logic\n" " */\n" " $this->_postInsert();\n\n" " /**\n" " * Update the _cleanData to reflect that the data has been inserted.\n" " */\n" " $this->_refresh();\n\n" " return $primaryKey;\n" " }\n\n" " /**\n" " * @return mixed The primary key value(s), as an associative array if the\n" " * key is compound, or a scalar if the key is single-column.\n" " */\n" " protected function _doUpdate()\n" " {\n" " /**\n" " * A read-only row cannot be saved.\n" " */\n" " if ($this->_readOnly === true) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('This row has been marked read-only');\n" " }\n\n" " /**\n" " * Get expressions for a WHERE clause\n" " * based on the primary key value(s).\n" " */\n" " $where = $this->_getWhereQuery(false);\n\n" " /**\n" " * Run pre-UPDATE logic\n" " */\n" " $this->_update();\n\n" " /**\n" " * Compare the data to the modified fields array to discover\n" " * which columns have been changed.\n" " */\n" " $diffData = array_intersect_key($this->_data, $this->_modifiedFields);\n\n" " /**\n" " * Were any of the changed columns part of the primary key?\n" " */\n" " $pkDiffData = array_intersect_key($diffData, array_flip((array)$this->_primary));\n\n" " /**\n" " * Execute cascading updates against dependent tables.\n" " * Do this only if primary key value(s) were changed.\n" " */\n" " if (count($pkDiffData) > 0) {\n" " $depTables = $this->_getTable()->getDependentTables();\n" " if (!empty($depTables)) {\n" " $pkNew = $this->_getPrimaryKey(true);\n" " $pkOld = $this->_getPrimaryKey(false);\n" " foreach ($depTables as $tableClass) {\n" " $t = $this->_getTableFromString($tableClass);\n" " $t->_cascadeUpdate($this->getTableClass(), $pkOld, $pkNew);\n" " }\n" " }\n" " }\n\n" " /**\n" " * Execute the UPDATE (this may throw an exception)\n" " * Do this only if data values were changed.\n" " * Use the $diffData variable, so the UPDATE statement\n" " * includes SET terms only for data values that changed.\n" " */\n" " if (count($diffData) > 0) {\n" " $this->_getTable()->update($diffData, $where);\n" " }\n\n" " /**\n" " * Run post-UPDATE logic. Do this before the _refresh()\n" " * so the _postUpdate() function can tell the difference\n" " * between changed data and clean (pre-changed) data.\n" " */\n" " $this->_postUpdate();\n\n" " /**\n" " * Refresh the data just in case triggers in the RDBMS changed\n" " * any columns. Also this resets the _cleanData.\n" " */\n" " $this->_refresh();\n\n" " /**\n" " * Return the primary key value(s) as an array\n" " * if the key is compound or a scalar if the key\n" " * is a scalar.\n" " */\n" " $primaryKey = $this->_getPrimaryKey(true);\n" " if (count($primaryKey) == 1) {\n" " return current($primaryKey);\n" " }\n\n" " return $primaryKey;\n" " }\n\n" " /**\n" " * Deletes existing rows.\n" " *\n" " * @return int The number of rows deleted.\n" " */\n" " public function delete()\n" " {\n" " /**\n" " * A read-only row cannot be deleted.\n" " */\n" " if ($this->_readOnly === true) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('This row has been marked read-only');\n" " }\n\n" " $where = $this->_getWhereQuery();\n\n" " /**\n" " * Execute pre-DELETE logic\n" " */\n" " $this->_delete();\n\n" " /**\n" " * Execute cascading deletes against dependent tables\n" " */\n" " $depTables = $this->_getTable()->getDependentTables();\n" " if (!empty($depTables)) {\n" " $pk = $this->_getPrimaryKey();\n" " foreach ($depTables as $tableClass) {\n" " $t = $this->_getTableFromString($tableClass);\n" " $t->_cascadeDelete($this->getTableClass(), $pk);\n" " }\n" " }\n\n" " /**\n" " * Execute the DELETE (this may throw an exception)\n" " */\n" " $result = $this->_getTable()->delete($where);\n\n" " /**\n" " * Execute post-DELETE logic\n" " */\n" " $this->_postDelete();\n\n" " /**\n" " * Reset all fields to null to indicate that the row is not there\n" " */\n" " $this->_data = array_combine(\n" " array_keys($this->_data),\n" " array_fill(0, count($this->_data), null)\n" " );\n\n" " return $result;\n" " }\n\n" " public function getIterator()\n" " {\n" " return new \\ArrayIterator((array) $this->_data);\n" " }\n\n" " /**\n" " * Returns the column/value data as an array.\n" " *\n" " * @return array\n" " */\n" " public function toArray()\n" " {\n" " return (array)$this->_data;\n" " }\n\n" " /**\n" " * Sets all data in the row from an array.\n" " *\n" " * @param array $data\n" " * @return Zend_Db_Table_Row_Abstract Provides a fluent interface\n" " */\n" " public function setFromArray(array $data)\n" " {\n" " $data = array_intersect_key($data, $this->_data);\n\n" " foreach ($data as $columnName => $value) {\n" " $this->__set($columnName, $value);\n" " }\n\n" " return $this;\n" " }\n\n" " /**\n" " * Refreshes properties from the database.\n" " *\n" " * @return void\n" " */\n" " public function refresh()\n" " {\n" " return $this->_refresh();\n" " }\n\n" " /**\n" " * Retrieves an instance of the parent table.\n" " *\n" " * @return \\Zend\\Db\\Table\\TableAbstract\n" " */\n" " protected function _getTable()\n" " {\n" " if (!$this->_connected) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('Cannot save a Row unless it is connected');\n" " }\n" " return $this->_table;\n" " }\n\n" " /**\n" " * Retrieves an associative array of primary keys.\n" " *\n" " * @param bool $useDirty\n" " * @return array\n" " */\n" " protected function _getPrimaryKey($useDirty = true)\n" " {\n" " if (!is_array($this->_primary)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"The primary key must be set as an array\");\n" " }\n\n" " $primary = array_flip($this->_primary);\n" " if ($useDirty) {\n" " $array = array_intersect_key($this->_data, $primary);\n" " } else {\n" " $array = array_intersect_key($this->_cleanData, $primary);\n" " }\n" " if (count($primary) != count($array)) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"The specified Table '$this->_tableClass' does not have the same primary key as the Row\");\n" " }\n" " return $array;\n" " }\n\n" " /**\n" " * Retrieves an associative array of primary keys.\n" " *\n" " * @param bool $useDirty\n" " * @return array\n" " */\n" " public function getPrimaryKey($useDirty = true)\n" " {\n" " return $this->_getPrimaryKey($useDirty);\n" " }\n\n" " /**\n" " * Constructs where statement for retrieving row(s).\n" " *\n" " * @param bool $useDirty\n" " * @return array\n" " */\n" " protected function _getWhereQuery($useDirty = true)\n" " {\n" " $where = array();\n" " $db = $this->_getTable()->getAdapter();\n" " $primaryKey = $this->_getPrimaryKey($useDirty);\n" " $info = $this->_getTable()->info();\n" " $metadata = $info[\\Zend\\Db\\Table\\TableAbstract::METADATA];\n\n" " // retrieve recently updated row using primary keys\n" " $where = array();\n" " foreach ($primaryKey as $column => $value) {\n" " $tableName = $db->quoteIdentifier($info[\\Zend\\Db\\Table\\TableAbstract::NAME], true);\n" " $type = $metadata[$column]['DATA_TYPE'];\n" " $columnName = $db->quoteIdentifier($column, true);\n" " $where[] = $db->quoteInto(\"{$tableName}.{$columnName} = ?\", $value, $type);\n" " }\n" " return $where;\n" " }\n\n" " /**\n" " * Refreshes properties from the database.\n" " *\n" " * @return void\n" " */\n" " protected function _refresh()\n" " {\n" " $where = $this->_getWhereQuery();\n" " $row = $this->_getTable()->fetchRow($where);\n\n" " if (null === $row) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException('Cannot refresh row as parent is missing');\n" " }\n\n" " $this->_data = $row->toArray();\n" " $this->_cleanData = $this->_data;\n" " $this->_modifiedFields = array();\n" " }\n\n" " /**\n" " * Allows pre-insert logic to be applied to row.\n" " * Subclasses may override this method.\n" " *\n" " * @return void\n" " */\n" " protected function _insert()\n" " {\n" " }\n\n" " /**\n" " * Allows post-insert logic to be applied to row.\n" " * Subclasses may override this method.\n" " *\n" " * @return void\n" " */\n" " protected function _postInsert()\n" " {\n" " }\n\n" " /**\n" " * Allows pre-update logic to be applied to row.\n" " * Subclasses may override this method.\n" " *\n" " * @return void\n" " */\n" " protected function _update()\n" " {\n" " }\n\n" " /**\n" " * Allows post-update logic to be applied to row.\n" " * Subclasses may override this method.\n" " *\n" " * @return void\n" " */\n" " protected function _postUpdate()\n" " {\n" " }\n\n" " /**\n" " * Allows pre-delete logic to be applied to row.\n" " * Subclasses may override this method.\n" " *\n" " * @return void\n" " */\n" " protected function _delete()\n" " {\n" " }\n\n" " /**\n" " * Allows post-delete logic to be applied to row.\n" " * Subclasses may override this method.\n" " *\n" " * @return void\n" " */\n" " protected function _postDelete()\n" " {\n" " }\n\n" " /**\n" " * Prepares a table reference for lookup.\n" " *\n" " * Ensures all reference keys are set and properly formatted.\n" " *\n" " * @param \\Zend\\Db\\Table\\TableAbstract $dependentTable\n" " * @param \\Zend\\Db\\Table\\TableAbstract $parentTable\n" " * @param string $ruleKey\n" " * @return array\n" " */\n" " protected function _prepareReference(\\Zend\\Db\\Table\\TableAbstract $dependentTable, \\Zend\\Db\\Table\\TableAbstract $parentTable, $ruleKey)\n" " {\n" " $parentTableName = (get_class($parentTable) === 'Zend_Db_Table') ? $parentTable->getDefinitionConfigName() : get_class($parentTable);\n" " $map = $dependentTable->getReference($parentTableName, $ruleKey);\n\n" " if (!isset($map[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS])) {\n" " $parentInfo = $parentTable->info();\n" " $map[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS] = array_values((array) $parentInfo['primary']);\n" " }\n\n" " $map[\\Zend\\Db\\Table\\TableAbstract::COLUMNS] = (array) $map[\\Zend\\Db\\Table\\TableAbstract::COLUMNS];\n" " $map[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS] = (array) $map[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS];\n\n" " return $map;\n" " }\n\n" " /**\n" " * Query a dependent table to retrieve rows matching the current row.\n" " *\n" " * @param string|\\Zend\\Db\\Table\\TableAbstract $dependentTable\n" " * @param string OPTIONAL $ruleKey\n" " * @param \\Zend\\Db\\Table\\Select OPTIONAL $select\n" " * @return Zend_Db_Table_Rowset_Abstract Query result from $dependentTable\n" " * @throws Zend_Db_Table_Row_Exception If $dependentTable is not a table or is not loadable.\n" " */\n" " public function findDependentRowset($dependentTable, $ruleKey = null, \\Zend\\Db\\Table\\Select $select = null)\n" " {\n" " $db = $this->_getTable()->getAdapter();\n\n" " if (is_string($dependentTable)) {\n" " $dependentTable = $this->_getTableFromString($dependentTable);\n" " }\n\n" " if (!$dependentTable instanceof \\Zend\\Db\\Table\\TableAbstract) {\n" " $type = gettype($dependentTable);\n" " if ($type == 'object') {\n" " $type = get_class($dependentTable);\n" " }\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Dependent table must be a \\Zend\\Db\\Table\\TableAbstract, but it is $type\");\n" " }\n\n" " // even if we are interacting between a table defined in a class and a\n" " // table via extension, ensure to persist the definition\n" " if (($tableDefinition = $this->_table->getDefinition()) !== null\n" " && ($dependentTable->getDefinition() == null)) {\n" " $dependentTable->setOptions(array(\\Zend\\Db\\Table\\TableAbstract::DEFINITION => $tableDefinition));\n" " }\n\n" " if ($select === null) {\n" " $select = $dependentTable->select();\n" " } else {\n" " $select->setTable($dependentTable);\n" " }\n\n" " $map = $this->_prepareReference($dependentTable, $this->_getTable(), $ruleKey);\n\n" " for ($i = 0; $i < count($map[\\Zend\\Db\\Table\\TableAbstract::COLUMNS]); ++$i) {\n" " $parentColumnName = $db->foldCase($map[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS][$i]);\n" " $value = $this->_data[$parentColumnName];\n" " // Use adapter from dependent table to ensure correct query construction\n" " $dependentDb = $dependentTable->getAdapter();\n" " $dependentColumnName = $dependentDb->foldCase($map[\\Zend\\Db\\Table\\TableAbstract::COLUMNS][$i]);\n" " $dependentColumn = $dependentDb->quoteIdentifier($dependentColumnName, true);\n" " $dependentInfo = $dependentTable->info();\n" " $type = $dependentInfo[\\Zend\\Db\\Table\\TableAbstract::METADATA][$dependentColumnName]['DATA_TYPE'];\n" " $select->where(\"$dependentColumn = ?\", $value, $type);\n" " }\n\n" " return $dependentTable->fetchAll($select);\n" " }\n\n" " /**\n" " * Query a parent table to retrieve the single row matching the current row.\n" " *\n" " * @param string|\\Zend\\Db\\Table\\TableAbstract $parentTable\n" " * @param string OPTIONAL $ruleKey\n" " * @param \\Zend\\Db\\Table\\Select OPTIONAL $select\n" " * @return Zend_Db_Table_Row_Abstract Query result from $parentTable\n" " * @throws Zend_Db_Table_Row_Exception If $parentTable is not a table or is not loadable.\n" " */\n" " public function findParentRow($parentTable, $ruleKey = null, \\Zend\\Db\\Table\\Select $select = null)\n" " {\n" " $db = $this->_getTable()->getAdapter();\n\n" " if (is_string($parentTable)) {\n" " $parentTable = $this->_getTableFromString($parentTable);\n" " }\n\n" " if (!$parentTable instanceof \\Zend\\Db\\Table\\TableAbstract) {\n" " $type = gettype($parentTable);\n" " if ($type == 'object') {\n" " $type = get_class($parentTable);\n" " }\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Parent table must be a \\Zend\\Db\\Table\\TableAbstract, but it is $type\");\n" " }\n\n" " // even if we are interacting between a table defined in a class and a\n" " // table via extension, ensure to persist the definition\n" " if (($tableDefinition = $this->_table->getDefinition()) !== null\n" " && ($parentTable->getDefinition() == null)) {\n" " $parentTable->setOptions(array(\\Zend\\Db\\Table\\TableAbstract::DEFINITION => $tableDefinition));\n" " }\n\n" " if ($select === null) {\n" " $select = $parentTable->select();\n" " } else {\n" " $select->setTable($parentTable);\n" " }\n\n" " $map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey);\n\n" " // iterate the map, creating the proper wheres\n" " for ($i = 0; $i < count($map[\\Zend\\Db\\Table\\TableAbstract::COLUMNS]); ++$i) {\n" " $dependentColumnName = $db->foldCase($map[\\Zend\\Db\\Table\\TableAbstract::COLUMNS][$i]);\n" " $value = $this->_data[$dependentColumnName];\n" " // Use adapter from parent table to ensure correct query construction\n" " $parentDb = $parentTable->getAdapter();\n" " $parentColumnName = $parentDb->foldCase($map[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS][$i]);\n" " $parentColumn = $parentDb->quoteIdentifier($parentColumnName, true);\n" " $parentInfo = $parentTable->info();\n\n" " // determine where part\n" " $type = $parentInfo[\\Zend\\Db\\Table\\TableAbstract::METADATA][$parentColumnName]['DATA_TYPE'];\n" " $nullable = $parentInfo[\\Zend\\Db\\Table\\TableAbstract::METADATA][$parentColumnName]['NULLABLE'];\n" " if ($value === null && $nullable == true) {\n" " $select->where(\"$parentColumn IS NULL\");\n" " } elseif ($value === null && $nullable == false) {\n" " return null;\n" " } else {\n" " $select->where(\"$parentColumn = ?\", $value, $type);\n" " }\n\n" " }\n\n" " return $parentTable->fetchRow($select);\n" " }\n\n" " /**\n" " * @param string|\\Zend\\Db\\Table\\TableAbstract $matchTable\n" " * @param string|\\Zend\\Db\\Table\\TableAbstract $intersectionTable\n" " * @param string OPTIONAL $callerRefRule\n" " * @param string OPTIONAL $matchRefRule\n" " * @param \\Zend\\Db\\Table\\Select OPTIONAL $select\n" " * @return Zend_Db_Table_Rowset_Abstract Query result from $matchTable\n" " * @throws Zend_Db_Table_Row_Exception If $matchTable or $intersectionTable is not a table class or is not loadable.\n" " */\n" " public function findManyToManyRowset($matchTable, $intersectionTable, $callerRefRule = null,\n" " $matchRefRule = null, \\Zend\\Db\\Table\\Select $select = null)\n" " {\n" " $db = $this->_getTable()->getAdapter();\n\n" " if (is_string($intersectionTable)) {\n" " $intersectionTable = $this->_getTableFromString($intersectionTable);\n" " }\n\n" " if (!$intersectionTable instanceof \\Zend\\Db\\Table\\TableAbstract) {\n" " $type = gettype($intersectionTable);\n" " if ($type == 'object') {\n" " $type = get_class($intersectionTable);\n" " }\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Intersection table must be a \\Zend\\Db\\Table\\TableAbstract, but it is $type\");\n" " }\n\n" " // even if we are interacting between a table defined in a class and a\n" " // table via extension, ensure to persist the definition\n" " if (($tableDefinition = $this->_table->getDefinition()) !== null\n" " && ($intersectionTable->getDefinition() == null)) {\n" " $intersectionTable->setOptions(array(\\Zend\\Db\\Table\\TableAbstract::DEFINITION => $tableDefinition));\n" " }\n\n" " if (is_string($matchTable)) {\n" " $matchTable = $this->_getTableFromString($matchTable);\n" " }\n\n" " if (! $matchTable instanceof \\Zend\\Db\\Table\\TableAbstract) {\n" " $type = gettype($matchTable);\n" " if ($type == 'object') {\n" " $type = get_class($matchTable);\n" " }\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Match table must be a \\Zend\\Db\\Table\\TableAbstract, but it is $type\");\n" " }\n\n" " // even if we are interacting between a table defined in a class and a\n" " // table via extension, ensure to persist the definition\n" " if (($tableDefinition = $this->_table->getDefinition()) !== null\n" " && ($matchTable->getDefinition() == null)) {\n" " $matchTable->setOptions(array(\\Zend\\Db\\Table\\TableAbstract::DEFINITION => $tableDefinition));\n" " }\n\n" " if ($select === null) {\n" " $select = $matchTable->select();\n" " } else {\n" " $select->setTable($matchTable);\n" " }\n\n" " // Use adapter from intersection table to ensure correct query construction\n" " $interInfo = $intersectionTable->info();\n" " $interDb = $intersectionTable->getAdapter();\n" " $interName = $interInfo['name'];\n" " $interSchema = isset($interInfo['schema']) ? $interInfo['schema'] : null;\n" " $matchInfo = $matchTable->info();\n" " $matchName = $matchInfo['name'];\n" " $matchSchema = isset($matchInfo['schema']) ? $matchInfo['schema'] : null;\n\n" " $matchMap = $this->_prepareReference($intersectionTable, $matchTable, $matchRefRule);\n\n" " for ($i = 0; $i < count($matchMap[\\Zend\\Db\\Table\\TableAbstract::COLUMNS]); ++$i) {\n" " $interCol = $interDb->quoteIdentifier('i' . '.' . $matchMap[\\Zend\\Db\\Table\\TableAbstract::COLUMNS][$i], true);\n" " $matchCol = $interDb->quoteIdentifier('m' . '.' . $matchMap[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS][$i], true);\n" " $joinCond[] = \"$interCol = $matchCol\";\n" " }\n" " $joinCond = implode(' AND ', $joinCond);\n\n" " $select->from(array('i' => $interName), array(), $interSchema)\n" " ->joinInner(array('m' => $matchName), $joinCond, \\Zend\\Db\\Select::SQL_WILDCARD, $matchSchema)\n" " ->setIntegrityCheck(false);\n\n" " $callerMap = $this->_prepareReference($intersectionTable, $this->_getTable(), $callerRefRule);\n\n" " for ($i = 0; $i < count($callerMap[\\Zend\\Db\\Table\\TableAbstract::COLUMNS]); ++$i) {\n" " $callerColumnName = $db->foldCase($callerMap[\\Zend\\Db\\Table\\TableAbstract::REF_COLUMNS][$i]);\n" " $value = $this->_data[$callerColumnName];\n" " $interColumnName = $interDb->foldCase($callerMap[\\Zend\\Db\\Table\\TableAbstract::COLUMNS][$i]);\n" " $interCol = $interDb->quoteIdentifier(\"i.$interColumnName\", true);\n" " $interInfo = $intersectionTable->info();\n" " $type = $interInfo[\\Zend\\Db\\Table\\TableAbstract::METADATA][$interColumnName]['DATA_TYPE'];\n" " $select->where($interDb->quoteInto(\"$interCol = ?\", $value, $type));\n" " }\n\n" " $stmt = $select->query();\n\n" " $config = array(\n" " 'table' => $matchTable,\n" " 'data' => $stmt->fetchAll(Zend_Db::FETCH_ASSOC),\n" " 'rowClass' => $matchTable->getRowClass(),\n" " 'readOnly' => false,\n" " 'stored' => true\n" " );\n\n" " $rowsetClass = $matchTable->getRowsetClass();\n" " if (!class_exists($rowsetClass)) {\n" " try {\n" " require_once 'Zend/Loader.php';\n" " Zend_Loader::loadClass($rowsetClass);\n" " } catch (Zend_Exception $e) {\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException($e->getMessage(), $e->getCode(), $e);\n" " }\n" " }\n" " $rowset = new $rowsetClass($config);\n" " return $rowset;\n" " }\n\n" " /**\n" " * Turn magic function calls into non-magic function calls\n" " * to the above methods.\n" " *\n" " * @param string $method\n" " * @param array $args OPTIONAL \\Zend\\Db\\Table\\Select query modifier\n" " * @return Zend_Db_Table_Row_Abstract|Zend_Db_Table_Rowset_Abstract\n" " * @throws Zend_Db_Table_Row_Exception If an invalid method is called.\n" " */\n" " public function __call($method, array $args)\n" " {\n" " $matches = array();\n\n" " if (count($args) && $args[0] instanceof \\Zend\\Db\\Table\\Select) {\n" " $select = $args[0];\n" " } else {\n" " $select = null;\n" " }\n\n" " /**\n" " * Recognize methods for Has-Many cases:\n" " * findParent<Class>()\n" " * findParent<Class>By<Rule>()\n" " * Use the non-greedy pattern repeat modifier e.g. \\w+?\n" " */\n" " if (preg_match('/^findParent(\\w+?)(?:By(\\w+))?$/', $method, $matches)) {\n" " $class = $matches[1];\n" " $ruleKey1 = isset($matches[2]) ? $matches[2] : null;\n" " return $this->findParentRow($class, $ruleKey1, $select);\n" " }\n\n" " /**\n" " * Recognize methods for Many-to-Many cases:\n" " * find<Class1>Via<Class2>()\n" " * find<Class1>Via<Class2>By<Rule>()\n" " * find<Class1>Via<Class2>By<Rule1>And<Rule2>()\n" " * Use the non-greedy pattern repeat modifier e.g. \\w+?\n" " */\n" " if (preg_match('/^find(\\w+?)Via(\\w+?)(?:By(\\w+?)(?:And(\\w+))?)?$/', $method, $matches)) {\n" " $class = $matches[1];\n" " $viaClass = $matches[2];\n" " $ruleKey1 = isset($matches[3]) ? $matches[3] : null;\n" " $ruleKey2 = isset($matches[4]) ? $matches[4] : null;\n" " return $this->findManyToManyRowset($class, $viaClass, $ruleKey1, $ruleKey2, $select);\n" " }\n\n" " /**\n" " * Recognize methods for Belongs-To cases:\n" " * find<Class>()\n" " * find<Class>By<Rule>()\n" " * Use the non-greedy pattern repeat modifier e.g. \\w+?\n" " */\n" " if (preg_match('/^find(\\w+?)(?:By(\\w+))?$/', $method, $matches)) {\n" " $class = $matches[1];\n" " $ruleKey1 = isset($matches[2]) ? $matches[2] : null;\n" " return $this->findDependentRowset($class, $ruleKey1, $select);\n" " }\n\n" " require_once 'Zend/Db/Table/Row/Exception.php';\n" " throw new \\Zend\\Db\\Table\\Row\\RowException(\"Unrecognized method '$method()'\");\n" " }\n\n\n" " /**\n" " * _getTableFromString\n" " *\n" " * @param string $tableName\n" " * @return \\Zend\\Db\\Table\\TableAbstract\n" " */\n" " protected function _getTableFromString($tableName)\n" " {\n" " return \\Zend\\Db\\Table\\TableAbstract::getTableFromString($tableName, $this->_table);\n" " }\n\n" "}\n") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html