This is similar to Making an acronym, but there are several key differences, including the method of fetching the acronym, and this challenge including flexible output.
Task
Given a string (list of chars/length 1 strings is allowed) containing only printable ASCII, output all capital letters in the input that are either preceded by a space or a dash, or are the first character in the input. Empty string is undefined behavior.
Test Cases:
Output may be in the format of "TEST", ["T","E","S","T"], or whatever else works for you.
Self-contained Underwater Breathing Apparatus
SUBA
a Programming Language
PL
NATO Atlantic TREATY Organization
NATO
DEFCON 2
D
hello, world!
light-Emitting dioDe
E
What Does the Fox Say?
WDFS
3D mov-Ies
I
laugh-Out Lou-D
OLD
Best friends FOREVE-r
BF
--
<space>
-- --a - - --
-- -- - - -- A
A
Step-Hen@Gmail-Mail Mail.CoM m
SHMM
This is code-golf, so shortest answer in bytes wins.
34 Answers 34
Ruby, (削除) 55 (削除ここまで) (削除) 30 (削除ここまで) 29 + 1 = (削除) 56 (削除ここまで) (削除) 31 (削除ここまで) 30 bytes
Run with -e flag (+1)
p (?-+$_).scan(/[ -]([A-Z])/)
Explanation: this is much easier as a regex if there's a hyphen in the beginning to match
Note: this returns a 2d list of characters like [["S"], ["U"], ["B"], ["A"]], which should be fine because
Output may be in the format of "TEST", ["T","E","S","T"], or whatever else works for you.
-
\$\begingroup\$ Use a negative lookbehind regex to save 3 bytes. \$\endgroup\$Value Ink– Value Ink2019年07月26日 20:22:33 +00:00Commented Jul 26, 2019 at 20:22
SNOBOL4 (CSNOBOL4), 86 bytes
S =' ' INPUT
S S ANY(' -') ANY(&UCASE) . X REM . S :F(O)
O =O X :(S)
O OUTPUT =O
END
S =' ' INPUT ;* prepend a space
S S ANY(' -') ANY(&UCASE) . X REM . S :F(O) ;* find an initial
O =O X :(S) ;* add to output string
O OUTPUT =O ;* print
END
[email protected]. \$\endgroup\$