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.
Don't match full string regex
MBartlett21
Posts: 2,064 ✭✭✭✭✭
How do i not match a full string in regex (I want to extract numbers out of it)
0
Best Answers
-
If you include a capture group in your regexp e.g.
[a-z]^([1-9]+), the value of that group will be in the resulting "captures" array.
From the match() documentation:captures {array} : The first element is always the input string s. The following elements are a list of all captured groups
5 -
Adding to Kevin's response: you can always add .* on both sides of a regexp to make it match a substring. You should also look at REGEX_NUMBER_CAPTURE -- that can help you extract real numbers (not just integers) from a regexp.
Finally, a much more reliable alternative to parsing strings is the lambda-based expression evaluation that Mahir and I used for the parametric curve and surface features: https://cad.onshape.com/documents/578ff8b3e4b0e65410fcfda3/w/d33395f174e5b38f4abd6097/e/0c17de6a800d4aed83de417fIlya Baran \ VP, Architecture and FeatureScript \ Onshape Inc5
Answers
-
Hi @mbartlett21
Perhaps add an example string that you would like to match and some examples that should not match? Are you saying you would like a regex that matches any string that contains at least one number? Or contains all numbers? Or something else?0 -
@mthiesmeyer
I would like a function to evaluate a string (like javascript eval()).
Example string: "x^2 + y^0.5".
I was wanting to use something like this to get the numbers in the string0 -
If you include a capture group in your regexp e.g.
[a-z]^([1-9]+), the value of that group will be in the resulting "captures" array.
From the match() documentation:captures {array} : The first element is always the input string s. The following elements are a list of all captured groups
5 -
Adding to Kevin's response: you can always add .* on both sides of a regexp to make it match a substring. You should also look at REGEX_NUMBER_CAPTURE -- that can help you extract real numbers (not just integers) from a regexp.
Finally, a much more reliable alternative to parsing strings is the lambda-based expression evaluation that Mahir and I used for the parametric curve and surface features: https://cad.onshape.com/documents/578ff8b3e4b0e65410fcfda3/w/d33395f174e5b38f4abd6097/e/0c17de6a800d4aed83de417fIlya Baran \ VP, Architecture and FeatureScript \ Onshape Inc5 -
@ilya_baran
How does the Expression type work?0 -
It's all defined in that feature studio. Basically, the idea is that an Expression is just a lambda function, say of one variable, t. The operators are overloaded for Expression types so that when you, for example, add two Expressions, the result is a function that evaluates the two functions and returns their sum. Constants are just turned into functions that return that constant, and t_ is the identity function. So when you write t_ + 3, you get a function that returns 3 plus its argument. More complicated expressions also just turn into functions that evaluate them.Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc0
-
@ilya_baran
Is it faster evaluating through a few functions to get the value, or would functions that returned reverse polish notation be faster?0 -
Neither will be terribly fast. I'm not sure which is faster -- you'd have to measure. I have a vague plan of using operator/function overloading to construct an expression tree in FS and have builtin functions compile it using LLVM, but that's far out.Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc0
-
-
Business Systems and Configuration Controller
HWM-Water Ltd0 -
Ilya, would there be any way to have an input like
annotation { "Name" : "Input", "Variables" : ["var1","var1"]} definition.input is Expression;
0 -
Here is my evalExpression script now (it first splits the tokens, turns that to Reverse Polish Notation, then evaluates it. It also allows contxts and lists of variables to be input, like x, y, etc.):
https://cad.onshape.com/documents/429fd7dda5f0fdb93833a34a
0




