# Awarded: ICDevs.org - Bounty #33 - Reg Ex Utility Canister - Rust - $4,000

**URL:** <https://forum.dfinity.org/t/awarded-icdevs-org-bounty-33-reg-ex-utility-canister-rust-4-000/17886>\
**Category:** Bounties & RFPs\
**Created:** [January 9, 2023, 8:59pm UTC](https://forum.dfinity.org/t/awarded-icdevs-org-bounty-33-reg-ex-utility-canister-rust-4-000/17886 "2023-01-09T20:59:30Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![ttt](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ttt/32/9010_2.png) [@ttt](https://forum.dfinity.org/u/ttt)\
**Post date:** [January 18, 2023, 9:09pm UTC](https://forum.dfinity.org/t/awarded-icdevs-org-bounty-33-reg-ex-utility-canister-rust-4-000/17886/4 "2023-01-18T21:09:04Z")

</div>

> [@skilesare](#):
>
> For the first version, I think it is safe to assume that each user than needs this will deploy their own canister so that cycle management isn’t an issue.

Great!

> [@skilesare](#):
>
> I probably need to better understand what you mean here. If I send a text and matching pattern you need to compile that particular matching patter? Is this an expensive operation?

Yes, compiling regex each time will definitely waste a lot of cycles. I also started implementation with saving regexes by name. But this is not friendly API (requires to keep track of compiled regexes and handle name clashes)  
Instead I decided to go with `precompile` function, which will store compiled regex by it’s sha256 in a hashmap, so any matching function could lookup already compiled version (or compile regex in place if cached version is missing).

My proposed API:

```auto
type Match = record {
    text: text;
    start: nat64;
    end: nat64;
};

type Re = text; // regex text

service : {
    "precompile": (vec Re) -> (); // idempotent method, call during initialization or before large batch of calls
    "is_match": (Re, text) -> (bool) query;
    "captures": (Re, text) -> (vec opt Match) query;
    "batch_is_match": (Re, vec text) -> (vec bool) query;
    "batch_captures": (Re, vec text) -> (vec vec opt Match) query;
};

```

> [@skilesare](#):
>
> `public query func com_regex_match([(pattern, text)]) : async matches;`

Should we support multiple patterns in the same call? I think we can skip this and go straight to exposing [RegexSet in regex - Rust](https://docs.rs/regex/latest/regex/struct.RegexSet.html) API, which matches several patterns in a single pass.

---

_[View the full topic](https://forum.dfinity.org/t/awarded-icdevs-org-bounty-33-reg-ex-utility-canister-rust-4-000/17886)._
