Regex reviser

Every now and then I need to use regex ( and I always forget/confuse what things like * + etc mean ). So this is just a little reviser with a few examples to help me as a reference. Hope you find it useful also. I’ve only covered the basics here as thats usually all I need.

special characters

+ match 1 or more of the previous character

* match 0 or more of the previous character

? previous character is optional

. match any character (except new line)

^ match starting with (also used as the NOT operator with classes eg [^0-9] , this means not a number )

$ match ending with

classes

[a-z] match any lower case char

[A-Z] match any upper case char

[0-9] match any number

examples

 

recently I was working on a site todo with actors and needed to find all the actor profile pages in the database (they had links like this) :

ahatalent.localhost/actor/alison-lintott
ahatalent.localhost/actor/finn-den-hertog

So I wrote a pattern to find them in text:

/ahatalent.localhost\/actor\/[a-z]+/                    match one or more a-z chars after /actor/ , but this broke due to the – alison after alison (as is non a-z char)

/ahatalent.localhost\/actor\/.+/                            I changed to use the wildcard to match 1 or more of any character after the /actor/

I could also have written:

/ahatalent.localhost\/actor\/(.+)/                   this the () just means evaluate this first ( same as in php or any most programming languages)

Example 2 (match a string that has 4 or 5 characters exactly)

Lets say I want to find all the aria-hiddens and eg aria-hidden=”true” or aria-hidden=”false” in the following bit of html:

        <p ng-show="actor.joneslink" class="tablet-display <?php if(!isset($data['joneslink']) && empty($data['joneslink'])){ echo ' ng-hide ';}?> " aria-hidden="true">Voiceover: <a href="<?php echo $data['joneslink']; ?>" target="_blank">The Joneses</a></p>
                  <?php } ?>
                  
                  <?php if($data['training']){ ?> 
                  <p ng-show="actor.training"  class="ng-binding <?php if(!isset($data['training']) && empty($data['training'])){ echo ' ng-hide ';}?> " aria-hidden="true"><?php echo $data['training']; ?></p>
                  <?php } ?>
                  
                  <?php if($data['eyecolour']){ ?> 
                  <p ng-show="actor.eyecolour"  class="ng-binding" aria-hidden="false"><?php echo $data['eyecolour']; ?></p>
                  <?php } ?>
                  
                  <?php if($data['height']){ ?>

My pattern could be this:

/aria-hidden=”.{4,5}”/

This means find this string: aria-hidden=” followed by 4 or 5 of any character. So would match both:

  • aria-hidden=”true”
  • aria-hidden=”false”

Example 3

Match strings between a given length.

Lets say I want to find strings that are between 5 and 10 chars in length. Maybe I want to find numbers between 5 and 10 chars in length in some text.

e.g. 12345, 54321, 1234567890 any string like that

my pattern could be:

/[0-9]{5,10}/

 

 

 

 

Leave a Comment