19
\$\begingroup\$

You are going to be given a string containing some alphabetic characters along with [ and ]. Your task is to split into sections that are enclosed in a "group" created by [...] and those that are not. e.g.

"absbn[mesl]meslo[eyyis]me"
->
"absbn"
"mesl"
"meslo"
"eyyis"
"me"

Super simple right? Well hold your horses. What happens when multiple brackets conflict? like co[[x]t or m[[e]]it? We don't have any guarantee that the brackets are matched or not nested.

The rule we are going to use is that when multiple possible groups could be made we will always choose the smallest ones. Any [s left without a match are just regular characters and appear in the output.

So in co[[x]t we could do co [x t, but we could also do co[ x t. Since the group here is either [x or x we choose the smaller one and the result is co[ x t. In the second case me[[e]]it there are 4 ways to make groups here, but unambiguously me[ e ]it results in the smallest group.

Your task will be to take a non-empty string of characters a through z plus two brackets of your choice ([], {}, () and <>) and to split it as described above, providing a list of strings as output.

In cases where a group is on the boundary of the string, e.g. aaa[b] or [ems]ee you may choose to include or omit an empty string "" on that boundary. The test cases always include them.

This is . Answers will be scored in bytes with the goal being to minimize the size of your source code.

Test cases

go[cat]me -> "go" "cat" "me"
absbn[mesl]meslo[eyyis]me -> "absbn" "mesl" "meslo" "eyyis" "me"
co[[x]t -> "co[" "x" "t"
me[[e]]t -> "me[" "e" "]t"
mon[g]]u -> "mon" "g" "]u"
msy]mesl -> "msy]mesl"
eoa[m -> "eoa[m"
a[b[ -> "a[b["
mesi]mmp[mo -> "mesi]mmp[mo"
meu[ems[mela[] -> "meu[ems[mela" "" ""
w[[[] -> "w[[" "" ""
[foo]foobar[bar] -> "" "foo" "foobar" "bar" ""
[][] -> "" "" "" "" ""
\$\endgroup\$
2
  • \$\begingroup\$ @DLosc Yep. Thanks. I shouldn't have been doing these test cases by hand. \$\endgroup\$
    – Wheat Wizard
    Jan 17 at 17:09
  • \$\begingroup\$ Please tell me there's a programming language that has this as a part of its syntax… (Or maybe rather don't?) \$\endgroup\$
    – xigoi
    Jan 17 at 22:33

8 Answers 8

7
\$\begingroup\$

Retina 0.8.2, 9 bytes

S`<(\w*)>

Try it online! Link includes test suite that switches between [] and <> and separates the output lines for each case with - for convenience. Explanation: Simply splits on a minimal group, capturing the contents.

The question now requires empty chunks, but the previous 10-byte version only required an extra _ prefix to disable them:

_S`<(\w*)>

Try it online! Link includes test suite that switches between [] and <> and separates the output lines for each case with - for convenience. Explanation: Splits on a minimal group, capturing the contents even if it is empty, but skipping empty chunks.

\$\endgroup\$
0
4
\$\begingroup\$

R, 68 63 62 bytes

Edit: -1 byte thanks to pajonk

function(x,`[`=gsub)"@"["",scan(t="<(\\w*)>"[" @\\1 ",x],,"")]

Try it online!

Would be 55 bytes if it wasn't for the annoying behaviour of R's strsplit function, which outputs an extra empty string if the split pattern occurs at the start of the string-to-be-split.

We first search for bracket-pairs (using < and > as brackets) that contain only letters between them, and swap these brackets for spaces. Then, we use scan to split the resulting string (because of the annoying strsplit behaviour). Scan has it's own problem, though, which is that it treats consecutive whitespace as a single delimiter, and so would miss any empty groups: so, we add a spurious @ character to each group, and then remove it with gsub when we're done splitting.

\$\endgroup\$
2
  • \$\begingroup\$ -1 byte by renaming gsub, which you had at some point in your answer already ;-) \$\endgroup\$
    – pajonk
    Jan 16 at 7:26
  • \$\begingroup\$ @pajonk - Aargh! Yes - thanks! As you've probably realized from the edit history, this felt to me more like a battle against gsub, strsplit and scan than against the challenge itself... \$\endgroup\$ Jan 16 at 9:18
4
\$\begingroup\$

sed, 92 21 20 bytes

s/<\(\w*\)>/\
\1\
/g

Try it online!

Thanks to DLosc for pointing out the specs had changed, greatly simplifying my answer, and for a -3 bytes golf

-1 thanks to "emanresu A" for pointing out I could use <> as my bracket chars

Prints each group on its own line.

  • Finds all matches of the form [<string of chars without brackets>].
  • Replaces them with \n<string of chars without brackets>\n
\$\endgroup\$
4
  • 1
    \$\begingroup\$ The OP has updated the specification, which I believe means you don't have to special-case anything anymore. Also, \w should work in place of [^][]. \$\endgroup\$
    – DLosc
    Jan 17 at 17:22
  • \$\begingroup\$ That makes things much nicer, thanks! \$\endgroup\$
    – Jonah
    Jan 17 at 17:40
  • 1
    \$\begingroup\$ -2 if you use <> which don't require escaping (I think?) \$\endgroup\$
    – emanresu A
    Jan 18 at 2:26
  • \$\begingroup\$ @emanresuA Thanks, I missed that we could do that. \$\endgroup\$
    – Jonah
    Jan 18 at 2:43
3
\$\begingroup\$

Python3, 158 bytes:

import re
def f(l):
 t=''
 while l:
  if(j:=re.findall('^\[\w+\]|^\[\]',l)):yield t;yield j[0][1:-1];l=l[len(j[0]):];t=''
  else:t+=l[0];l=l[1:]
 if t:yield t

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You don't need to quote ]s in regexes. \$\endgroup\$
    – Neil
    Jan 15 at 18:45
3
\$\begingroup\$

Pip -p, 11 bytes

a^`<(\w*)>`

Uses <> for brackets. Attempt This Online!

Verify all test cases

Explanation

Same idea as Neil's Retina answer: the smallest possible group is always one that does not include any brackets.

a            ; Command-line input
 ^           ; Split
  `       `  ; on this regex:
   <         ;   Opening bracket
    (   )    ;   Group the following part so it is included in the list of results:
     \w*     ;     0 or more word characters (here, letters)
         >   ;   Closing bracket
\$\endgroup\$
3
\$\begingroup\$

Javascript, 113 103 39 bytes

s=>s.replace(/<(\w*)>/g,'0$10').split(0)

Uses <> as the brackets.

First time code golfing, yay! I'm pretty sure there's some regex magic which will beat this, but this was the best I could come up with.

EDIT: First version didn't properly handle strings starting or ending with groups, e.g. <a>b<c>

EDIT2: Shaved off 10 bytes thanks to emanresu A

EDIT3: Figured out the regex magic

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to the site! I don't golf in JS, but you might be able to find some useful tips from those who do on our tips for golfing in JS question. \$\endgroup\$
    – Wheat Wizard
    Jan 15 at 17:17
  • 1
    \$\begingroup\$ Array.from(x) can be [...x], and you can save a few more with 0 instead of _. \$\endgroup\$
    – emanresu A
    Jan 16 at 5:30
2
\$\begingroup\$

Python, 83 bytes

lambda s:[t for i,t in enumerate(re.split(r"\[([^][]*)\]",s))if(i&1)or t]
import re

Attempt This Online!

This uses re.split which returns a list containing both the stuff between matches and those parts of matches that are in grouping parentheses.

This is then filtered to remove empty strings but only between groups; empty groups are retained.

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 57 bytes

s/<(\w*)>/ $1 /g;s/^ (.*) $/$1/;print for split/ +/,$_,-1

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.