Oldie but Goldie -- the mythic regexps by Mark_S

As mentioned elsewhere, here is @Mark_S’ bestest plugin.

$__core_modules_filters_regexps.js.json (2.5 KB)

TiddlyWiki has several regexp related operators.

Original response, which missed the "tone". I'm sorry about that one It is **not recommended to use an undocumented 3rd party filter operator** that is very similar in name as the existing ones.

Edit:
I think it is important to add some info or a link to the documentation, so users know how a filter operator should be used.

Honestly, that feels very personal.

Where are these recommendations written down? Who else’s code should be disqualified?

3 Likes

EDIT: Removed.

While I understand the inclination towards avoiding 3rd-party filter operators, they are nonetheless a vital part of the TiddlyWiki ecosystem and are often needed to fill in some of the gaps in the TWCore functionality.

@Mark_S 's regexps[] filter operator does something that the existing TWCore regexp related operators only do with some difficulty. Specifically, it extracts the matched pattern from the input.

For example, suppose we have an input like this:

<$let input="This sentence has a number 123 embedded in it">

To get that number from the input using the TWCore, you could write the following somewhat complex filter:

{{{ [<input>search-replace:string:regexp[\D*(\d+)\D*],[$1]] }}}

In comparison, using Mark’s regexps[] filter operator is MUCH simpler. You just write:

{{{ [<input>regexps[\d+]] }}}

which is significantly easier to understand, and does not rely on needing a capture group nor the \D* prefix/suffix to exclude the rest of the non-numeric string content.

Also note that Mark’s regexps[] operator was created way back in 2017, long before the TWCore’s search-replace operator, which was only added in v5.1.23 (24th December 2020). Similarly, the other regexp related operators you referenced – splitregexp[] and escaperegexp[] – were only added in v5.1.20 (9th August 2019) and v5.1.14 (26th April 2017), respectively… and neither of those filter operators do anything like what Mark’s regexps[] operator does.

Before Mark’s regexps[] operator, the only TWCore filter for handling regular expressions was regexp[], which only checks for “match or not match”, and can’t extract the matched text from the input.

Although I agree that the regexps[] name is “very similar” to existing TWCore operators, the functionality it provides is very useful and distinct from those other operators, and I have long felt that it should have been added as a standard TWCore filter operator… though perhaps with a different name, such as extract[].

-e

4 Likes

Absolutely. In Orthodox Regex jargon it handles Capturing Groups well.

I do think it would benefit from a different name, if only to prevent confusion when quickly scanning a filter. I really like extract as an alternative, and I may rename it for my own use — particularly if it doesn’t seem likely to make it into the core. (Alas.)

Names aside, though, this seems like a deeply useful operator! Thank you to @Mark_S for sharing your excellent work, to @CodaCoder for resurfacing this gem, and to @EricShulman for that excellent example.

capture[ ] or regcapture[ ]

?

I’m sorry. I didn’t want my post to feel personal. I wanted to point out that the posted JSON file has no information about how to use it. There is no link to any documentation, nor does the OP contain any information, how to use it.

I’ll formulate my post differently.

2 Likes

Unfortunately, there is prior use re extract (@telmiger)

https://tid.li/tw5/hacks.html#Extract%20Macro

It’s not an operator, but still…

Please do not hesitate (re-)using names from old macros of mine like this rather hacky one.

sieve – which is what filters should be called.

OCD? No, not all. I have CDO where the letters are in alphabetical order like they damn well should have been all along. :stuck_out_tongue_winking_eye:

1 Like