Help with regex syntax please

The following returns a syntax error:
{{{
[tag[w1]]
+[search:regexp[page [1-9]$]]
+[search:regexp[page [1-9][0-9]]]
}}}

I am trying to sort tiddlers with tens and units numbers in order

Unfortunately it is not possible to use some of the regex syntax as a literal parameter to the search:regexp operator, as these would conflict with the filter syntax. The square brackets are a common offender. One way to overcome this would be to store the regex expression in a variable:

<$let
  expression1="page [1-9]$"
  expression2="page [1-9][0-9]$">

{{{
[tag[w1]]
+[search:regexp<expression1>]
+[search:regexp<expression2>]
}}}

</$let>

Btw, it seems like there might be a simpler way to achieve what you want, without using regex at all. If all the titles you want to sort are prefixed page , followed by a number, then you could use a filter expression like this:

[prefix[page ]] :sort:integer[<currentTiddler>removeprefix[page ]]

You can read more about the :sort prefix here: https://tiddlywiki.com/#Sort%20Filter%20Run%20Prefix

2 Likes

As @vilc has pointed out, you can’t use square brackets with the regex expression because they conflict with the use of square brackets as delimiters for the filter operator syntax.

While his suggested solution (using variables to hold the regex patterns) is a common workaround for this issue, there is an alternative for your specific regex syntax that avoids using square brackets: \d will match any numeric digit [0-9], and {n,m} matches items between n and m characters in length.

Thus, you could write:

{{{ [tag[w1]search:title:regexp[page \d{1,2}$]] }}}

and this will match any titles that end with a 1- or 2-digit number (and also allows for leading 0 values such as “01”, “02”, etc

Of course, once you have this title list, you still need to sort it by the trailing numeric value. To do this, you can add a :sort:integer filter run, like this:

{{{ [tag[w1]search:title:regexp[page \d{1,2}$]] :sort:integer[<currentTiddler>split[ ]last[]]}}}

where the filter syntax within the :sort:integer run takes each title and splits it on the spaces to get the last non-space text, which will be the number portion of the title.

enjoy,
-e

2 Likes

Thank you so much for your responses, I am now much closer but not quite there, my titles are like the following, exactly the same text up to and including ‘page’ then a one or two digit page followed by nothing or 'left 'or ‘right’ or ‘left&right’

Account, William1 page 8
Account, William1 page 80 left
Account, William1 page 80 right
Account, William1 page 81 left
Account, William1 page 81 right
Account, William1 page 9

Can you help a little further please?

Can you simply use the alphnumeric sort operator, sortan, like this, [tag[w1]sortan[]]? That would give this order:

Account, William1 page 8
Account, William1 page 9
Account, William1 page 80 left
Account, William1 page 80 right
Account, William1 page 81 left
Account, William1 page 81 right
1 Like

That works perfectly, thank you.

Thank you your simpler way achieved what I wanted.

1 Like

Eric, thanks for your response, I am only getting pages 1 to 9 returned. Page 9 preceeds page 10 right so there is always just one space?

My suggestion was made before you noted the trailing “left”, “right” or “left&right” suffix requirement. Thus, my regexp pattern assumed that “page N” was always at the end of the title, and would fail to match “… page 10 right”.

Although @Scott_Sauyet 's successful solution is clearly simpler and more efficient, if you did want to allow handling of titles with varying prefixes AND optional trailing “left”, “right” or “left&right”,you could write:

{{{ [regexp[page \d{1,2}]]
   :sort:integer[<currentTiddler>trim[ left]trim[ right]trim[ left&right]split[ ]last[]] }}}

Notes:

  • I used the shorter (and older) regexp[...] filter operator, which is equivalent to search:title:regexp[...].
  • The regexp pattern omits the trailing $ (“end-of-text”) pattern, to allow for the optional “left”, “right” or “left&right” suffixes.
  • The trim[ ...] operators remove the trailing suffixes, including the space the precedes them.

-e

1 Like

Thanks again, I have learn’t a lot from this exchange and realise that there is much more to discover.