string match regex help

kai_liu589
kai_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_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,279 image
    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_liu589
    kai_liu589 Member, Developers Posts: 5 ✭
    Thanks Ilya!