Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Regex to parse array of integers
neobobkrause
Member Posts: 105 EDU
Sorry to bother, but I'm struggling to successfully parse a string representing an array of integers. you know, like this...
[1]
[1,0]
[5,3,7, 9, 23,394857]
And yes, I purposely added a space or two before some of the numbers. (It could be a user-entered value, and I want to be forgiving.)
I want each integer to be captured as a separate entry in the captures results array.
This is the regex expression I'm using and captures result...
{ captures : [ [0,1,3] , 0 , ,3 ] , hasMatch : true }
Any help would be appreciated.
- Bob
[1]
[1,0]
[5,3,7, 9, 23,394857]
And yes, I purposely added a space or two before some of the numbers. (It could be a user-entered value, and I want to be forgiving.)
I want each integer to be captured as a separate entry in the captures results array.
This is the regex expression I'm using and captures result...
{ captures : [ [0,1,3] , 0 , ,3 ] , hasMatch : true }
Any help would be appreciated.
- Bob
0
Best Answer
-
MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭@neobobkrause
I have a little library for splitting strings by a set of characters:
https://cad.onshape.com/documents/55a3567381b135db132be033
If you usesplit(string, ["[", ",", " ", "\t", "]"])
, that should give you an array of strings, where you can then just callstringToNumber
on each one of them.7
Answers
I have a little library for splitting strings by a set of characters:
https://cad.onshape.com/documents/55a3567381b135db132be033
If you use
split(string, ["[", ",", " ", "\t", "]"])
, that should give you an array of strings, where you can then just callstringToNumber
on each one of them.IR for AS/NZS 1100
Nice little library you have there. Thanks.
I am having a similar issue trying to scan this array of arrays containing both integers and strings for user input dependent variables. Do you think I should try some variation of your solution here? I've tried about everything I can think of to trick my script into thinking my UI input Hub Style Choice enum is a number and am not sure what else to do to stop the function getValues from erroring out. Screenshot of problem area included below. Public ink here as well:
https://cad.onshape.com/documents/4d960d010797369631360a38/w/5cb01732326da62448679d2f/e/8b93f0d5050b470b1bbda1ac?renderMode=0&uiState=64ed2ea01e82a66a62c54083
Note: I was originally reading in integer data directly from a CSV but had to add the Hub_Style_Choice search key and have reverted to scanning the array directly for ease in debugging (I am new to featurescript and coding in general).
Thanks in advance for any advice!