Browse the notebook

Tag: regular expressions

E4X Filtering

var userInfo:XML = <users>
  <user fname="joe" lname="smith" age="31" />
  <user fname="mildred" lname="calder" age="64" />
  <user fname="ben" lname="nathanson" age="20" />
  <user fname="james" lname="biuford" age="19" />
  <user fname="nick" lname="calhoun" age="45" />
</users>;


trace("Users over 20:n");
trace(userInfo.user.(@age > 20).toXMLString());

trace("nUsers with the name nick:n");
trace(userInfo.user.(@fname == "nick" ).toXMLString());

// use regular expressions with e4x
trace("nUsers with name starting with j:n");
trace(userInfo.user.(/^j/.test(@fname)));

/*
outputs:

Users over 20:

<user fname="joe" lname="smith" age="31"/>
<user fname="mildred" lname="calder" age="64"/>
<user fname="nick" lname="calhoun" age="45"/>

Users with the name nick:

<user fname="nick" lname="calhoun" age="45"/>

Users with name starting with j:

<user fname="joe" lname="smith" age="31"/>
<user fname="james" lname="biuford" age="19"/>

*/

One of the nicest features of E4X is filtering. The above code shows a few simple examples … the last example makes use of regular expressions — I first read using regular expressions and E4X somewhere on http://www.darronschall.com/.

I usually prefer to use a database for any kind of info I’ll be searching… but if you know you have a relatively small amount of data XML can be a fine way to go.

Read snippet: E4X Filtering

String.replace()

var words:String = "ActionSnippet.com is a website. ActionSnippet.com is a blog.";

// outputs: "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
trace(words);

// the "/g" tells it to replace all instances of ActionSnippet.com
words = words.replace(/ActionSnippet.com/g, "SomeWebsite.com");

// outputs: SomeWebsite.com is a website. SomeWebsite.com is a blog.
trace(words);

Just a quick response to a student question.

Read snippet: String.replace()