1

I'm trying to find a good regex to use with TextWrangler to parse task descriptions into a Rememberthemilk.com format: so that a line like:

Example task section 1 section tomorrow section tagone tagtwo tagthree tagfour 

whould convert into the RTM format:

Example task !1 ^tomorrow #tagone #tagtwo #tagthree #tagfour 

Now this gets me into the question of, how do you capture an infdefinitely-repeating pattern using the right syntax in the backreferences?

I've been trying this regex:

^([\w ]+) section (\d) section (\w+) section ((\w+) )+$

which seems to capture the format of the input text, but I just don't know how to make the conversion in the backreferences?

\1 !\2 ^\3 #(???)

How do I achieve this?

Elmar Peise
  • 14,014
  • 3
  • 21
  • 40
MiguelJazz
  • 11
  • 1
  • Each character in the replacement string gets used exactly once. There's no way to repeat the "#" for each match of the final group. If there were always the same number of tags, you could match them individually, but I'm assuming that's not the case. – jasonharper Jan 15 '17 at 23:54
  • I don't think you can achieve this just with replacement and backreferences. Doing this in a more fully-fledged programming language might let you do it (e.g. C# has the ability to access all captures for a particular group). Example: https://gist.github.com/Porges/9ba3b7f94f2c14e20feb16c984406e5a#file-example-cs – porges Jan 15 '17 at 23:55

1 Answers1

0

Capture quantified constructs, don't quantify capture groups.

here we also use "grouping only" parens (no capture group) on the inner section and capture the outer to get a clean result:

([\w ]+) section (\d) section (\w+) section ((?:\w+\s?)+)

you want the repeated constuct inside the capturing (), it doesn't make sense to "repeat the group", in most engines only the last result the group matched will be captured. (though some regex engines will allow a repeated capture group to remember all sub-matches (.NET for example))

demo: https://regex101.com/r/y3CryR/2

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43