Introduction

"And soon I will have understanding of videocassette recorders and car telephones. And when I have understanding of them, I shall have understanding of computers. And when I have understanding of computers, I shall be the Supreme Being!" (Time Bandits 1981)

Chris Ellis is a freelance Senior Flash Programmer, former Lingo Programmer and resident of London.

He has an MSc in Interactive Multimedia from Westminster University, graduating in 2000. He started programming at the age of 8 with a BBC Model B and a few books on BASIC.

What follows are some snippets from what he's currently up to.

Wednesday, 6 January 2010

A handy regular expression

Often I need to find a substring which can be easily done with a regular expression.

I find regular expressions really useful once I worked it out, but invaribly it takes me longer to work it out than to use substr or something similar

Hopefully this snippet will be handy for someone else, or at the very least a reference for me the next time I need to do it and have forgotten how it's done.

So, for example if my string is:
var videoName:String = "assets/videos/video_235.flv";
and I need to find the number of it (235) then a quick and easy way is to use a regular expression such as:
var regEx:RegExp = /assets\/videos\/video_([^;]+).swf/
trace(regEx.exec(videoName)[1]);
will display 235
The handy bit is:
([^;]+)

This will get the regex to return everything inbetween what you put infront and behind it.
The backslashes I used are just to escape the forward slashes in the url.

0 comments: