Tag

Regex Archives - Custom Web Application Developers in Bromley, London

Regex reviser

By | Pattern Matching, Programming, Regular Expressions | No Comments

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

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:

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