Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

string match regex help

kai_liu589kai_liu589 Member, Developers Posts: 5
edited June 2017 in FeatureScript
I have a string like this:
</code>"ALine,-93980000000,0,93980000000,0////AArc,0,0,93980000000,93980000000,0,-93980000000,0"</pre></span><span>I want to first split it by "////". Then split the resulting two groups by comma. I tried to use string match to accomplish this but it doesn't return any matches.<br></span><pre class="CodeBlock"><code>println(match("ALine,-93980000000,0,93980000000,0////AArc,0,0,93980000000,93980000000,0,-93980000000,0", "[^(////)]+"));&nbsp;
I then tried with a simple case:
<div>println(match("a~~aa~a", "a.")); <span style="background-color: transparent; color: inherit; font-size: inherit; font-family: Flama, sans-serif;">{ captures : [] , hasMatch : false } </span> </div> println(replace("a~~aa~a", "a.", "X"));<span><br>X~X~a</span>
For the same regex, the replace works by the match doesn't. What did I miss on using the match function?


p.s. I don't want to upload a csv file to do this.

Comments

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,211
    match checks whether the entire string matches, whereas replace works on substrings.

    Something like this might help, though it's not going to be too fast:

    function split(str is string, re is string)
    {
        var m = match(str, '(.*)' ~ re ~ '(.*)');
        if (!m.hasMatch)
            return [str];
        var rest = m.captures[1];
        var last = m.captures[2];
        return append(split(rest, re), last);
    }
    
    We'll expose more string processing functions in time.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • kai_liu589kai_liu589 Member, Developers Posts: 5
    Thanks Ilya!
Sign In or Register to comment.