RegExp

September 2nd, 2008 Leave a comment Go to comments

Special characters: . * ? + [ ] ( ) { } ^ $ | \

Grouping without backreferencing
/(?:Not Backreferenced)SomeTextToMatch(What I want to get backreferenced)/

Look ahead
/Look(?= ahead)/ Matches only ‘Look’ when it is followed by ‘ ahead’.
/Look(?! ahead)/ Matches only ‘Look’ when it is not followed by ‘ ahead’.

Look back
/(?< =Look )back)/ Matches only ‘back’ when it is preceded by ‘Look ‘.
/(?< !Look )back)/ Matches only ‘back’ when it is not preceded by ‘Look ‘.

RegExp special variables
$(num) Backreferences
$& Contains last successful match
$' Contains the substring following the last successful match
$` Contains the substring preceding the last successful match
@+ & @- I knew they were good for something! :)

Tricks
Getting all backreferences even though I do not know how many there will be.

my @array;
while ($string =~ /(SomethingToMatch):/g) {
      push @array, $1;
}

$1 can be substituted by $&

  1. No comments yet.
  1. No trackbacks yet.