Well... as usual I can't give a full introduction to balancing groups here. For a primer see my Stack Overflow answer see my Stack Overflow answer.
Well... as usual I can't give a full introduction to balancing groups here. For a primer see my Stack Overflow answer.
Well... as usual I can't give a full introduction to balancing groups here. For a primer see my Stack Overflow answer.
Unfortunately, this doesn't work on Try It Online, due to a bug in Mono.Try it online.
Unfortunately, this doesn't work on Try It Online, due to a bug in Mono.
Retina, (削除) 108 (削除ここまで) (削除) 102 (削除ここまで) (削除) 94 (削除ここまで) (削除) 87 (削除ここまで) (削除) 82 (削除ここまで) 64(削除) 64 (削除ここまで) 63 bytes
Massive thanks to Kobi who found a much more elegant solution, which allowed me to save another 1819 bytes on top of that.
S_`(?<=^(?<-1>.)*(?:.(?<=\G(.)*.).)+)
.
0ドル
m+`^(?=( *)\S.*\n1円)
<space>
S_`(?<=^(?<-1>.)*(?:.(?<=\G(.)*.).)+)
ThisThe first stage is a Split stage, which splits the input into segmentslines of increasing length. The This part changed significantly. I'll have to rewrite the explanation later._ indicates that empty chunks should be omitted from the splitting (which only affects the end, because there will be a match in the last position). The regex itself is entirely contained in a look-around so it won't match any characters, but only positions.
This part is based on Kobi's solution with some additional golfitude I found myself. Note that lookbehinds are matched right-to-left in .NET, so the following explanation should best be read bottom to top. I've also inserted another \G in the explanation for clarity, although that isn't necessary for the pattern to work.
(?<=
^ # And we ensure that we can reach the beginning of the stack by doing so.
# The first time this is possible will be exactly when tri(m-1) == tri(n-1),
# i.e. when m == n. Exactly what we want!
(?<-1>.)* # Now we keep matching individual characters while popping from group <1>.
\G # We've now matched m characters, while pushing i-1 captures for each i
# between 1 and m, inclusive. That is, group <1> contains tri(m-1) captures.
(?:
(?<=
\G # The \G anchor matches at the position of the last match.
(.)* # ...push one capture onto group <1> for each character between here
# here and the last match.
) # Then we use a lookahead to...
. # In each iteration we match a single character.
)+ # This group matches all the characters up to the last match (or the beginning
# of the string). Call that number m.
) # If the previous match was at position tri(n-1) then we want this match
# to happen exactly n characters later.
I'm still admiring Kobi's work here. This is even more elegant than the prime testing regex. :)
Retina, (削除) 108 (削除ここまで) (削除) 102 (削除ここまで) (削除) 94 (削除ここまで) (削除) 87 (削除ここまで) (削除) 82 (削除ここまで) 64 bytes
Massive thanks to Kobi who found a much more elegant solution, which allowed me to save another 18 bytes on top of that.
S_`(?<=^(?<-1>.)*(?:.(?<=\G(.)*.))+)
.
0ドル
m+`^(?=( *)\S.*\n1円)
<space>
S_`(?<=^(?<-1>.)*(?:.(?<=\G(.)*.))+)
This splits the input into segments of increasing length. This part changed significantly. I'll have to rewrite the explanation later.
Retina, (削除) 108 (削除ここまで) (削除) 102 (削除ここまで) (削除) 94 (削除ここまで) (削除) 87 (削除ここまで) (削除) 82 (削除ここまで) (削除) 64 (削除ここまで) 63 bytes
Massive thanks to Kobi who found a much more elegant solution, which allowed me to save another 19 bytes on top of that.
S_`(?<=^(?<-1>.)*(?:(?<=\G(.)*).)+)
.
0ドル
m+`^(?=( *)\S.*\n1円)
<space>
S_`(?<=^(?<-1>.)*(?:(?<=\G(.)*).)+)
The first stage is a Split stage, which splits the input into lines of increasing length. The _ indicates that empty chunks should be omitted from the splitting (which only affects the end, because there will be a match in the last position). The regex itself is entirely contained in a look-around so it won't match any characters, but only positions.
This part is based on Kobi's solution with some additional golfitude I found myself. Note that lookbehinds are matched right-to-left in .NET, so the following explanation should best be read bottom to top. I've also inserted another \G in the explanation for clarity, although that isn't necessary for the pattern to work.
(?<=
^ # And we ensure that we can reach the beginning of the stack by doing so.
# The first time this is possible will be exactly when tri(m-1) == tri(n-1),
# i.e. when m == n. Exactly what we want!
(?<-1>.)* # Now we keep matching individual characters while popping from group <1>.
\G # We've now matched m characters, while pushing i-1 captures for each i
# between 1 and m, inclusive. That is, group <1> contains tri(m-1) captures.
(?:
(?<=
\G # The \G anchor matches at the position of the last match.
(.)* # ...push one capture onto group <1> for each character between here
# here and the last match.
) # Then we use a lookahead to...
. # In each iteration we match a single character.
)+ # This group matches all the characters up to the last match (or the beginning
# of the string). Call that number m.
) # If the previous match was at position tri(n-1) then we want this match
# to happen exactly n characters later.
I'm still admiring Kobi's work here. This is even more elegant than the prime testing regex. :)