Difference between revisions of "Regular Expressions"

From wiki
Jump to navigation Jump to search
Line 28: Line 28:
 
;$var =~ /<pattern>/
 
;$var =~ /<pattern>/
 
:Generic syntax, this expression is true if the pattern is matched in $var
 
:Generic syntax, this expression is true if the pattern is matched in $var
 +
 +
;@array = $var =~ m/<pattern>/g;
 +
:Put all matches of <pattern> in var into @array
  
 
Following variables are when a match is made:
 
Following variables are when a match is made:
Line 53: Line 56:
 
     && ($rev = $+);
 
     && ($rev = $+);
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
==Python==
 
==Python==
 
Check [[Python:Strings#Regular_Expressions]]
 
Check [[Python:Strings#Regular_Expressions]]

Revision as of 14:34, 9 July 2018

Regular expressions or regexp are used to find strings in text.

. 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

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 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