TwelvestoneFlash

AS3 Regex woes


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
JBall
 
2011-03-07

Anyone here experienced with regex expressions in AS3? I am trying to get a regex match on HTML code that is parsed into a text box in flash. I have successfully loaded the HTML code and then began the regex matching, but am stuck on getting the right regex expression to match.

The code that I am using in html page builds a tree view list on the html page, so once it's loaded into the flash text box, i am trying to match the html pages to build a list from. The code below is what I am trying to match. The code I want to match is the first two items in quotes for each entry to build the array list of page names, and their URL. The problem is that the pages won't be named as nice as the example below. Each time it can be a different page name, and page url, so I need to match what is in the first two quotes.

I can't find any examples of the regex expression to do this.

insertEntry(aux2, NewLink("Page1", "Page1.html", "page")) insertEntry(aux2, NewLink("Page2", "Page2.html", "page")) insertEntry(aux2, NewLink("Page3", "Page3.html", "page")) insertEntry(aux2, NewLink("Page4", "Page4.html", "page"))

persist
 
2011-03-07

This is a bizarre data transport format.

Why not format it when it is sent to Flash rather than trying to parse code?

lithium
 
2011-03-07

It's kind of hacky, but it works with the examples you provided. Hopefully it'll give you a shove in the right direction...

var lines : Array = [ 'insertEntry(aux2, NewLink("Page1", "Page1.html", "page"))', 'insertEntry(aux2, NewLink("Page2", "Page2.html", "page"))', 'insertEntry(aux2, NewLink("Page3", "Page3.html", "page"))', 'insertEntry(aux2, NewLink("Page4", "Page4.html", "page"))' ];

function parse ( line : String ) : Object { var results : Array = line.match( /(["'])["']*\1/g ); if ( results.length > 1 ) { return { name : results[0].replace(/\"/g, ""), url : results[1].replace(/\"/g, "") }; }

return null;

}

var list : Array = []; var i : int;

for ( i = 0; i < lines.length; i++ ) { var line : String = lines[i]; var parsed : Object = parse ( line );

if ( parsed ) list.push ( parsed );

}

for ( i = 0; i < list.length; i++ ) { trace ( "Name -> " + list[i].name + ", URL -> " + list[i].url ); }

persist
 
2011-03-08

Whoaa nice. Maybe prune it a bit though.

This should suffice:

function getPagesFromInserts(out){ out = out.replace(/", "/g,"\",\"").split("NewLink("); //Cleans extra space & finds links specifically in case incoming has garbage, considering incoming is already sketchy for(var i = 1; i<out.length;i++){ //Iterate through links found out[i] = out[i].split("))")[0].replace(/\"/g,"").split(","); //find members and organize as array } out.shift();//Remove remaining garbage from the first split return out; }

/*usage: //string: var test = 'insertEntry(aux2, NewLink("Page1", "Page1.html", "page"))insertEntry(aux2, NewLink("Page2", "Page2.html", "page"))insertEntry(aux2, NewLink("Page3", "Page3.html", "page"))insertEntry(aux2, NewLink("Page4", "Page4.html", "page"))';

var outMan = getPagesFromInserts(test); */

I didn't strict type it. sue me. Human compiled. Sorry if sucks in advance. Potentially returns bad arrays if contains no NewLink instances, but jasus, I'm a doctor not a geologist! My use of regex here is also a bit of a poke. You can use a simple string for a global replace, but the man asked for regex. k

This should work in JS, before Flash ever sees it too. If one were so inclined. I'd format the data as close to it's origin as possible so if someday data can be produced in another way that doesn't include extra substrings you don't have to fix it in more than one place.

Alternatvely with a library such as d.eval for as3 you could actually run the code with some minor tweaking like semicolon statement endings and defining aux2 as a var - This would only be more economical than string finds in this case if there is a VERY large number of NewLink instances.

JBall
 
2011-03-08

Thanks guys this was very helpful. Persist, the format is is janky. it's output from an authoring tool to build an indented list table of contents that builds the list in a format similar to the code below.

The unfortunate thing I have been trying to deal with is the name of the page titles and page.html for each item will not be the same everytime as they can be dynamically changed when a new course is created, so I couldn't make a regex match on something hard set each time.

That code will help get me int he right direction. Thanks again, you guys are amazing.

<!--

if( parent.toc15325 ) scrollWnd=parent.toc15325 if( scrollWnd ) { cssFile = 'toc15325.css' if( !is.ns4 ) ancStyle = 'text-decoration:none;font-size:13px; font-family:"Arial", sans serif; color:#000000;' showIcons = false treeLines = 0 initialMode = 2

fT = NewFolder("Table Of Contents", "", null) insertEntry(fT, NewLink("Requirements", "requirements.html", "page")) aux1 = insertFolder(fT, NewFolder("Title", "title_introduction.html", "chap")) insertEntry(aux1, NewLink("Introduction", "title_introduction.html", "page")) aux2 = insertFolder(aux1, NewFolder("Administration", "administration_welcome.html", "sect")) insertEntry(aux2, NewLink("Welcome", "welcome.html", "page")) insertEntry(aux2, NewLink("Navigation", "navigation.html", "page")) insertEntry(aux2, NewLink("Copyright Information", "copyright_information.html", "page")) insertEntry(aux2, NewLink("Menu", "menu.html", "page")) aux2 = insertFolder(aux1, NewFolder("Module Title", "module_title.html", "sect")) insertEntry(aux2, NewLink("Module Title", "module_title.html", "page")) insertEntry(aux2, NewLink("Module One Objectives", "module_one_objectives.html", "page")) insertEntry(aux2, NewLink("Page One", "page_one.html", "page")) insertEntry(aux2, NewLink("Page Two", "page_two.html", "page")) insertEntry(aux2, NewLink("Page Three", "page_three.html", "page")) insertEntry(aux2, NewLink("Check for Understanding 1.1", "check_for_understanding_1.1.html", "page")) insertEntry(aux2, NewLink("Module One Summary", "module_one_summary.html", "page")) aux1 = insertFolder(fT, NewFolder("Final Exam", "exam_overview.html", "test")) insertEntry(aux1, NewLink("Overview", "overview.html", "page")) insertEntry(aux1, NewLink("Question 1", "exam_question_1.html", "page")) insertEntry(aux1, NewLink("Question 2", "exam_question_2.html", "page")) insertEntry(aux1, NewLink("Question 3", "exam_question_3.html", "page")) insertEntry(aux1, NewLink("Question 4", "exam_question_4.html", "page")) insertEntry(aux1, NewLink("Question 5", "exam_question_5.html", "page")) scrollWnd.foldertree = fT

rewritepage() }

// -->

persist
 
2011-03-14

well that's an unfortunately script. So close to ecma, yet to much like vb to do evals.

I hope you get things working well enough.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

AS3 Regex woes