2

I have to parse out color information from HTML data. The colors can either be RGB colors or file names to a swatch image.

I used http://www.gskinner.com/RegExr/ to develop and test the patterns. I copied the AS regular expression code verbatim from the tool into Flex Builder. But, when I exec the pattern against the string I get a null.

Here are the patterns and an example of the string (I took the correct HTML tags out so the strings would show correctly):

DIV data:

<div style="background-color:rgb(2,2,2);width:10px;height:10px;">

DIV pattern:

/([0-9]{1,3},[0-9]{1,3},[0-9]{1,3})/

IMG data:

<img src="/media/swatches/jerzeesbirch.gif" width="10" height="10" alt="Birch">

IMG pattern:

/[a-z0-9_-]+/[a-z0-9_-]+/[a-z0-9_-]+\.[a-z0-9_-]+/

Here's my Actionscript code:

var divPattern : RegExp = new RegExp("/([0-9]{1,3},[0-9]{1,3},[0-9]{1,3})/");
var imgPattern : RegExp = new RegExp("/[a-z0-9_-]+/[a-z0-9_-]+/[a-z0-9_-]+\.[a-z0-9_-]+/");

var divResult : Array = divPattern.exec(object.swatch);
var imgResult : Array = imgPattern.exec(object.swatch);  

Both of the arrays are null.

This is my first foray into AS coding, so I think I'm declaring something wrong.

Steve

2
  • I assume you've checked that trace(object.swatch); returns a valid string? Commented Feb 15, 2010 at 13:51
  • +1 for gskinner.com/RegExr ... Commented Feb 15, 2010 at 16:04

2 Answers 2

3

(I don't know ActionScript but I know Javascript and they should be close enough to solve your problem.)


To construct a RegExp object for e.g. the pattern ^[a-z]+$, you either use

var pattern : RegExp = new RegExp("^[a-z]+$");

or, better,

var pattern : RegExp = /^[a-z]+$/

The code new RegExp("/^[a-z]+$/") is wrong because this expects a slash before the ^ and after the $.


Therefore, your DIV pattern should be written as

var divPattern : RegExp = /([0-9]{1,3},[0-9]{1,3},[0-9]{1,3})/;

but, as you know, the ( and ) are special characters for capturing, you need to escape them:

var divPattern : RegExp = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/;

For the IMG pattern, as / delimitates a RegEx, you need to escape it as well:

var imgPattern : RegExp = /[a-z0-9_-]+\/[a-z0-9_-]+\/[a-z0-9_-]+\.[a-z0-9_-]+/

Finally, you could use \d in place of [0-9] and \w in place of [a-zA-Z0-9_].

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know enough to tell if your regex patterns are correct, but from the docs on the AS3 RegExp class, it looks like your new RegExp() call needs a second argument to declare flags for case sensitivity etc.

EDIT: Also, as Bart K has pointed out, you don't need the / delimiters when using the new method.

So you can use either:

var divPattern:RegExp = new RegExp("([0-9]{1,3},[0-9]{1,3},[0-9]{1,3})", "");

OR you can also use the alternate syntax with /:

var divPattern:RegExp = /([0-9]{1,3},[0-9]{1,3},[0-9]{1,3})/;

... in which case the flag string (if any) is included after the final /

4 Comments

No, when using new new RegExp(...) no delimiters are needed (see the link you posted).
You can omit the () delimiters, but if you DO use them you I think you need to supply a second argument (even if it's an empty string as shown in Adobe's example). And spdaly's code uses the delimiters.
No, I was talking about the / delimiters. Instantiating a RegExp is done by doing either var pattern:RegExp = /\w/; or var pattern:RegExp = new RegExp("\w");. The new RegExp(... syntax expects a plain string (without the delimiters!). See: livedocs.adobe.com/flex/3/html/…
Aahh - see what you mean now, thanks for pointing that out. I will update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.