How to remove leading digits from the start of a string of text

Twine Version: 2.9.1
Harlowe 3.3.9

Folks, I have been using Twine for almost two months and learned my way around Harlowe programming pretty well, but, pattern searching is quite strange compared to other languages.

I have a list of items preceded with 1 to 3 digit numbers like:

  1. Hello
  2. Dude
  3. Weather

And I simply want to strip away the number and period. With any other language I would just use a regular expression pattern and replace it with null text.

This is what I had to do in Harlowe:

(set: _t to “233. This is a test”)
(set: _t2 to (trimmed: (p-start: digit), _t))
(set: _t2 to (trimmed: (p-start: digit), _t2))
(set: _t2 to (trimmed: (p-start: digit), _t2))
(set: _t to (trimmed: (p-start: “.”), _t2))

This returns: “This is a test.”

Instead of 4 lines of code, I should be able to do this in 1 line. The three duplicated commands will each trim one digit at the start of the text. So up to three digits are trimmed, then the fourth command removes the decimal.

Anyone have a better way to do this? I must be missing something.

Poking around the docs a bit…

(p-start: ...digit, ".", (p-opt: whitespace)) might be the pattern you want?

2 Likes

Thanks very much. I didn’t know the spread operator could be used with digit.
Starting with variable _t set to 324. Testing
This returns just Testing

(set: _t2 to (trimmed: (p-start: …digit,“.”), _t))
(print: _t2)

I have to admit some of the document is not clear while other parts of it are very well explained.

1 Like