Regular Expressions: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 11: | Line 11: | ||
|^||Start of string||$||End of string | |^||Start of string||$||End of string | ||
|- | |- | ||
|*||0 or more matches of previous expression | |*||0 or more matches of previous expression||( )||Subexpression | ||
|- | |- | ||
|<nowiki>+</nowiki>||1 or more matches of previous expression | |<nowiki>+</nowiki>||1 or more matches of previous expression||[ ]||Match anything between the [ ].<br>^as first character negates the match | ||
|- | |- | ||
|<nowiki>?</nowiki>||0 or 1 matches | |<nowiki>?</nowiki>||0 or 1 matches of previous expression.<br>Stop search as soon as next expression is found (non greedy) | ||
as soon as next expression is found (non greedy) | |||
|} | |} | ||
| Line 32: | Line 31: | ||
: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 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. | :The string following whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already.<br>Example: | ||
<syntaxhighlight lang=perl> | <syntaxhighlight lang=perl> | ||
$_ = 'abcdefghi'; | $_ = 'abcdefghi'; | ||
/def/; | /def/; | ||
print "$`:$&:$' | print "$`:$&:$'"; | ||
# prints abc:def:ghi | # prints abc:def:ghi | ||
</syntaxhighlight> | </syntaxhighlight> | ||
;$1 | |||
:String matched by the first subexpression. | |||
;$+ | ;$+ | ||
: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. | :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.<br>Example: | ||
<syntaxhighlight lang=perl> | <syntaxhighlight lang=perl> | ||
/Version: (.*)|Revision: (.*)/ | /Version: (.*)|Revision: (.*)/ | ||
Revision as of 08:00, 14 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 | ( ) | Subexpression |
| + | 1 or more matches of previous expression | [ ] | Match anything between the [ ]. ^as first character negates the match |
| ? | 0 or 1 matches of previous expression. 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.
Example:
$_ = 'abcdefghi';
/def/;
print "$`:$&:$'";
# prints abc:def:ghi
- $1
- String matched by the first subexpression.
- $+
- 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.
Example:
/Version: (.*)|Revision: (.*)/
&& ($rev = $+);