PHP » PYTHON |
login |
register |
about
|
PYTHON preg_match
is this article helpful?
|
Python replacement for PHP's preg_match
[
edit
| history
]
import re matches = re.search(pattern, subject) matches = re.search(pattern, subject, flags) https://docs.python.org/2/library/re.html preg_match(PHP 4, PHP 5) preg_match — Perform a regular expression match Description
int preg_match
( string $pattern
, string $subject
[, array &$matches
[, int $flags
[, int $offset
]]] )
Searches subject for a match to the regular expression given in pattern . Parameters
Return Valuespreg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred. Changelog
Examples
Example #1 Find the string of text "php"
<?php
Example #2 Find the word "web"
<?php
Example #3 Getting the domain name out of a URL
<?php The above example will output: domain name is: php.net
Example #4 Using named subpattern
<?php The above example will output: Array ( [0] => foobar: 2008 [name] => foobar [1] => foobar [digit] => 2008 [2] => 2008 )
NotesSee Also
|
more
Recently updated
more
Most requested
more
Last requests
|