Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-06-2012, 05:24 PM   #1
tnel
Registered User
 
Join Date: Jun 2012
Posts: 35
Default What's wrong with this regular expression?

I am trying to parse a string using the split method by separating the string anytime there is any number of spaces, a section of spaces followed by a comma, or just a comma. I am trying to use a regular expression to do it but it isn't quite right. Here is what I have:

Code:
private var txtString:String = "a,b,c,d,e     ,12,13,15    16,6   ,7";

var criteria:RegExp = /[\s*|,|(\s*,)]/
array = txtString.split(criteria);

//output:
a
b
c
d
e






//desired output:
a
b
c
d
e
12
13
15
16
6
7
I know the Regular Expression is wrong but I'm not quite sure how to modify it to get the desired results. This is the first time I've used regular expressions so they are a bit foreign. Also, is there another way to achieve the same result?

Thanks.

Last edited by tnel; 07-06-2012 at 05:34 PM. Reason: Fixing.
tnel is offline   Reply With Quote
Old 07-06-2012, 06:27 PM   #2
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

/[\s*|,|(\s*,)]/

first, you're brackets is a 1 character OR search... it just confuses your search, remove those

/\s*|,|(\s*,)/

That still doesn't solve your problem though... so lets break down your piped possibilities

* means 0->MANY greedy. Well you don't want to separate 12 into 1 and 2, so 0 is pointless here. But we DO want to grab every , may it be preceeded by or followed by 0->MANY spaces. So swap \s*|, with \s*,\s*

/\s*,\s*|(\s*,)/

and lastly this makes (\s*,) redundant, BUT you do need blank spaces with no commas. + does a search of 1-MANY so replace that with \s+ and you get:

/\s*,\s*|\s+/

that should work... I personally tested on this string and it worked:

ActionScript Code:
var txtString:String = "a, b,c, d,e     ,12 ,  13,15    16,6   ,7";

(note I put some more spaces in there before and after commas to test that better)
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!

Last edited by lordofduct; 07-06-2012 at 09:00 PM.
lordofduct is offline   Reply With Quote
Old 07-06-2012, 08:55 PM   #3
tnel
Registered User
 
Join Date: Jun 2012
Posts: 35
Default It works.

That worked perfectly. Thank you for your help and enabling me to understand Regular Expression syntax a bit better!
tnel is offline   Reply With Quote
Old 07-17-2012, 06:00 PM   #4
tnel
Registered User
 
Join Date: Jun 2012
Posts: 35
Default Adding an additional constraint

On the above problem I now would like to add an additional parameter that splits antime there is a return. I'm not sure if I should be using \n or \r, but neither of the following seem to work:

Code:
var criteria:RegExp = /\s*,\s*|\s+|\r/;
Code:
var criteria:RegExp = /\s*,\s*|\s+|\n/;
tnel is offline   Reply With Quote
Old 07-17-2012, 06:22 PM   #5
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

well we probably want the return line to also except whitespace around it... so \s*\n\s* is what we'll want

but because either of that whitespace could be registered by the \s+, we need this to take precedence over it... so put it BEFORE the \s+

and we get:

Code:
/\s*,\s*|\s*\n\s*|\s+/
or, because the , and \n are so similar you could redact further to:

Code:
/\s*[,\n]\s*|\s+/
if you wanted other delimiters, you could stick them in that bracket... also excepting * and ~ as delimiters would be (note how I escape the * because * means something special in regex):

Code:
/\s*[,\n\*~]\s*|\s+/
\n should catch most return lines, but it might not catch all... not sure where you're strings are being created. But I tested it on this string and it worked fine:

ActionScript Code:
var txtString:String = "a, b,c, d\ne   \n  ,12 ,  13,15    16,6   ,7";
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!

Last edited by lordofduct; 07-17-2012 at 06:27 PM.
lordofduct is offline   Reply With Quote
Old 07-17-2012, 07:22 PM   #6
tnel
Registered User
 
Join Date: Jun 2012
Posts: 35
Default Columns of Data

Thank you for the help. That is working for most cases but there are a few instances I can't figure out. The data I need parsed is starting as 8 columns of data in Excel that are saved as a .csv file and embedded into an actionscript class. The column headers for the 8 columns are shown here:

Code:
time    id    type    amount    projection    diff    cumulativeDiff
The kicker is that several of the cells are blank so when I have the following code:

Code:
var data:Object = new EmbeddedText();
trace(data.toString());

var criteria:RegExp = /\s*[,\n]\s*|\s+/;
var dataArray:Array = data.toString().split(criteria);
trace(dataArray);
the output from the trace(data.toString()); line looks like this in some cases:

time,id,type,amount,projection,diff,cumulativeDiff
,,M,75,17,,,
,,300,7,,,

So inbetween the values 17 and 300 above it seems like the regular expression should split the code six times: one for each of the three commas on the line after the 17, one for the return to go to the next line, and two for the commas appearing before the value 300. However, the actual result is:

time,id,type,amount,projection,diff,cumulativeDiff ,,M,75,17,,,,,L,300,7,,,

Now I've managed to get one more comma between 'cumulativeDiff' and 'M' by using the regular expression:

Code:
var criteria:RegExp = /\s*\n\s*|\s*,\s*|\s+/
but that isn't picking up the extra space between '17' and 'L'.
tnel is offline   Reply With Quote
Old 07-17-2012, 07:39 PM   #7
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

What is the data between those though?

Try going through the string and finding out what characters are ACTUALLY there, and that'll help you find out what you need to search for.

Look at the byte info of the string. You could use a bytearray to loop through and get the char codes in raw format (yeah, you could use the string and ascii and the sort... but that could introduce some confusion/errors because of all the conversions, where as a bytearray would be raw data)
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!
lordofduct is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:20 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.