PHP » PYTHON |
login |
register |
about
|
PYTHON preg_match_all
is this article helpful?
|
Python replacement for PHP's preg_match_all
[
edit
| history
]
import re matchesIter = re.finditer(pattern, subject) matchesIter = re.finditer(pattern, subject, flags) # or matches = re.findall(pattern, subject) matches = re.findall(pattern, subject, flags) preg_match_all(PHP 4, PHP 5) preg_match_all — Perform a global regular expression match Description
int preg_match_all
( string $pattern
, string $subject
, array &$matches
[, int $flags
[, int $offset
]] )
Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags . After the first match is found, the subsequent searches are continued on from end of the last match. Parameters
Return ValuesReturns the number of full pattern matches (which might be zero), or FALSE if an error occurred. Changelog
Examples
Example #1 Getting all phone numbers out of some text.
<?php
Example #2 Find matching HTML tags (greedy)
<?phpThe above example will output: matched: <b>bold text</b> part 1: <b> part 2: bold text part 3: </b> matched: <a href=howdy.html>click me</a> part 1: <a href=howdy.html> part 2: click me part 3: </a>
Example #3 Using named subpattern
<?phpThe above example will output:
Array
(
[0] => Array
(
[0] => a: 1
[1] => b: 2
[2] => c: 3
)
[name] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
[digit] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
See Also
|
more
Recently updated
more
Most requested
|