Difference between revisions of "Regular Expressions"
Jump to navigation
Jump to search
(→Perl) |
|||
Line 46: | Line 46: | ||
&& ($rev = $+); | && ($rev = $+); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | ==Python== | ||
+ | Check [[Python:Strings#Regular_Expressions]] |
Revision as of 12:12, 11 March 2018
. | Any character except newline | \c | Control character |
\d | Digit | \D | non Digit |
\s | Whitespace | \S | non Whitespace |
\w | Word character [A-Za-z0-9] | \W | non Word character |
^ | Start of string | $ | End of string |
* | 0 or more matches of previous expression | ||
+ | 1 or more matches of previous expression | ||
? | 0 or 1 matches (optional). Stop search
as soon as next expression is found (non greedy) |
Perl
- $var =~ /<pattern>/
- Generic syntax, this expression is true if the pattern is matched in $var
Following variables are when a match is made:
- $&
- Contains the string matched by the last pattern match
- $`
- The string preceding whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already.
- $'
- The string following whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already. For example:
$_ = 'abcdefghi';
/def/;
print "$`:$&:$'n";
# prints abc:def:ghi
- $+
- The last bracket matched by the last search pattern. This is useful if you don't know which of a set of alternative patterns matched. For example:
/Version: (.*)|Revision: (.*)/
&& ($rev = $+);