Regular Expressions: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (11 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Regular expressions or regexp are used to find strings in text. | |||
[[Perl]], [[PHP]] and [[Python]] support all in below table. [[:Category:Bash|Bash]] and [[SQL]] only support the POSIX part. | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
|.||Any character except newline||\c||Control character | |.||Any character except newline (POSIX)||\c||Control character (not for Python3) | ||
|- | |- | ||
|\d||Digit||\D||non Digit | |\d||Digit||\D||non Digit | ||
| Line 9: | Line 13: | ||
|\w||Word character [A-Za-z0-9]||\W||non Word character | |\w||Word character [A-Za-z0-9]||\W||non Word character | ||
|- | |- | ||
|^||Start of string||$||End of string | |^||Start of string (POSIX)||$||End of string (POSIX) | ||
|- | |- | ||
|*||0 or more matches of previous expression | |*||0 or more matches of previous expression (POSIX)||( )||Subexpression (POSIX) | ||
|- | |- | ||
|<nowiki>+</nowiki>||1 or more matches of previous expression | |<nowiki>+</nowiki>||1 or more matches of previous expression (POSIX)||[ ]||Match any of the characters between the [ ].<br>^as first character negates the match (POSIX) | ||
|- | |- | ||
|<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) (POSIX)||{x,y}||Match the previous expression between x an y times.<br>When y is ommited match exactly x times (POSIX) | ||
as soon as next expression is found (non greedy) | |- | ||
|<nowiki>\n</nowiki>||Newline||<nowiki>\<int></nowiki>||Refers to the <int>-th subexpression (POSIX) | |||
|} | |} | ||
==Examples== | |||
;<code>/[^/]*$</code> | |||
:Match last / and everything following | |||
==Perl== | |||
;perl -lne 'print $1 if (/<regexp(subexp)>/)' | |||
:Commandline to print the first subexp in a match. | |||
;$var =~ /<pattern>/ | |||
:Generic syntax, this expression is true if the pattern is matched in $var | |||
;@array = $var =~ m/<pattern>/g; | |||
:Put all matches (or all first submatches) of <pattern> in var into @array | |||
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.<br>Example: | |||
<syntaxhighlight lang=perl> | |||
$_ = 'abcdefghi'; | |||
/def/; | |||
print "$`:$&:$'"; | |||
# prints abc:def:ghi | |||
</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.<br>Example: | |||
<syntaxhighlight lang=perl> | |||
/Version: (.*)|Revision: (.*)/ | |||
&& ($rev = $+); | |||
</syntaxhighlight> | |||
==Python== | |||
Check [[Python:Strings#Regular_Expressions_(regexp)]] and/or the [https://cheatography.com/mutanclan/cheat-sheets/python-regular-expression-regex/ cheatography site] | |||
Latest revision as of 14:24, 7 July 2025
Regular expressions or regexp are used to find strings in text.
Perl, PHP and Python support all in below table. Bash and SQL only support the POSIX part.
| . | Any character except newline (POSIX) | \c | Control character (not for Python3) |
| \d | Digit | \D | non Digit |
| \s | Whitespace | \S | non Whitespace |
| \w | Word character [A-Za-z0-9] | \W | non Word character |
| ^ | Start of string (POSIX) | $ | End of string (POSIX) |
| * | 0 or more matches of previous expression (POSIX) | ( ) | Subexpression (POSIX) |
| + | 1 or more matches of previous expression (POSIX) | [ ] | Match any of the characters between the [ ]. ^as first character negates the match (POSIX) |
| ? | 0 or 1 matches of previous expression. Stop search as soon as next expression is found (non greedy) (POSIX) |
{x,y} | Match the previous expression between x an y times. When y is ommited match exactly x times (POSIX) |
| \n | Newline | \<int> | Refers to the <int>-th subexpression (POSIX) |
Examples
/[^/]*$- Match last / and everything following
Perl
- perl -lne 'print $1 if (/<regexp(subexp)>/)'
- Commandline to print the first subexp in a match.
- $var =~ /<pattern>/
- Generic syntax, this expression is true if the pattern is matched in $var
- @array = $var =~ m/<pattern>/g;
- Put all matches (or all first submatches) of <pattern> in var into @array
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 = $+);
Python
Check Python:Strings#Regular_Expressions_(regexp) and/or the cheatography site