Regex (POSIX ERE / RE2 or better), 23 21 20 bytes
^\b((t?\But)?-tut)+$
Based on regexes by thejonymyster and Bubbler.
Try it on regex101! - RE2
Try it online! - GNU ERE (Bash + egrep)
Try it online! - ECMAScript
Try it online! - Perl
Try it online! - Raku:P5
Try it online! - PCRE
Try it online! - Java
Try it online! – Try It Online") - Boost
Try it online! - Python
Try it online! - Ruby
Try it online! - .NET
^ # Anchor to start of string.
\b # Assert there is a word boundary here. Given the context, this is
# equivalent to asserting that the first character isn't "-".
(
(
t? # Optionally match and consume "t".
\B # Assert that this is not a word boundary. Given the context, this
# is equivalent to asserting that the string does not start here.
ut # Match and consume "ut".
)? # Optionally match the above.
-tut # Match and consume "-tut".
)+ # Iterate the above as many times as possible, minimum one.
$ # Assert we've reached the end of the string.
Alternative 20 bytes:
^(tut-(tu\Bt?)?)+\b$
Try it on regex101! - RE2
Try it online! - ECMAScript
Try it online! - PCRE
This version doesn't work properly in GNU ERE; it matches tut-tuttut-tu
. This appears to be a bug in GNU ERE, and is still present in the latest version. Note that ^(a\Bb?){2}$
incorrectly matches aba
:
Try it online! - GNU ERE (bug) / Attempt This Online! (bug)
Try it online! - PCRE (no bug)
But ^a\Bb?a\Bb?$
correctly doesn't match aba
:
Try it online! - GNU ERE / Attempt This Online!
21 bytes without word-boundary assertions (GNU ERE or better):
^(tut-(tut?|))+tut$\2
21 bytes without word-boundary assertions or backreferences (ECMAScript or better):
^(?=t)((t?ut)?-tut)*$
Regex (ECMAScript or better), 22 bytes
^((?=tut-tut).{1,7})*$
Try it online! - ECMAScript
Try it online! - Perl
Try it online! - Raku:P5
Try it online! - PCRE
Try it online! - Java
Try it online! - Boost
Try it online! - Python
Try it online! - Ruby
Try it online! - .NET
^ # Anchor to start of string
(
(?=tut-tut) # Assert that the following 7 characters match "tut-tut",
# without consuming them.
.{1,7} # Skip anywhere from 1 to 7 characters
)* # Iterate the above as many times as possible
$ # Assert we've reached end of string
Alternative 22 bytes:
^(t(?=ut-tut).{0,6})*$
Regex (Perl v5.34+ / PCRE2 v10.43+ / Python / Ruby), 21 bytes
^((?=tut-tut).{,7})*$
Attempt This Online! - Perl v5.40+
Attempt This Online! - PCRE2 v10.44+
Try it online! - Python
Try it online! - Ruby
Python and Ruby support the quantifier shorthand of {,N}
to mean {0,N}
. So too do the latest versions of Perl and PCRE.
Alternative 21 bytes:
^(t(?=ut-tut).{,6})*$
Attempt This Online! - Perl v5.40+
Attempt This Online! - PCRE2 v10.44+
Try it online! - Python
Try it online! - Ruby
Regex (Raku), 25 21 bytesSBCS
^tut»((t?ut)?\-tut)+$
Ported from a previous (21 byte) version of the "POSIX ERE / RE2 or better" regex.
Try it online!
Porting the 21 byte lookahead-using version comes to 28 bytes:
^<?before t>((t?ut)?\-tut)*$
Try it online!
\$\large\textit{Anonymous functions}\$
{/^tut»((t?ut)?\-tut)+$/}
Try it online!
Ruby, 29 28 30 bytes
->s{s=~/^\b((t?\But)?-tut)+$/}
Try it online!
Julia v1.5+, 32 bytes
endswith(r"^\b((t?\But)?-tut)+")
Attempt This Online!
-5 bytes compared to v1.2+, thanks to MarcMush
Julia v1.2+, 39 38 37 bytes
s->endswith(s,r"^\b((t?\But)?-tut)+")
Attempt This Online!
JavaScript, 35 34 33 bytes
s=>/^\b((t?\But)?-tut)+$/.test(s)
Try it online!
$args-match'^\b((t?\But)?-tut)+$'
Try it online!
Java, 36 bytes
s->s.matches("((?=tut-tut).{1,7})*")
Try it online!
s->s.matches("tut\\b((t?ut)?-tut)+")
Try it online!
s->s.matches("\\b((t?\\But)?-tut)+")
Try it online!
R, 40 bytes
\(L)grepl('^((?=tut-tut).{1,7})*$',L,,1)
Attempt This Online!
\(L)grepl('^tut\\b((t?ut)?-tut)+$',L,,1)
Attempt This Online!
\(L)grepl('^\\b((t?\\But)?-tut)+$',L,,1)
Attempt This Online!
PHP, 47 bytes
fn($s)=>preg_match('/^\b((t?\But)?-tut)+$/',$s)
Try it online!
Python, 54 53 bytes
lambda s:re.match(r'\b((t?\But)?-tut)+$',s);import re
Try it online!
\$\large\textit{Full programs}\$
Retina, 21 20 bytes
^\b((t?\But)?-tut)+$
Try it online!
Pip, 25 24 21 bytes
+`(t?\But|\b)-tut`~=a
Try it online!
Thanks to DLosc for demonstrating this syntax. This is apparently shorthand for ^((t?\But|\b)-tut)+$
. This regex is slightly less inefficient speed-wise, but by moving all distinctive operations inside the loop, it allows this shortcut to be used.
Ruby -n
, 25 24 bytes
p~/^\b((t?\But)?-tut)+$/
Try it online!
Prints nil
for false and 0
for true, which are respectively falsey and truthy in Ruby. If printing 0
or 1
is desired, replace p~
with p !!
(+2 bytes).
Perl -p
, 27 26 25 bytes
$_=/^\b((t?\But)?-tut)+$/
Try it online!
MATL, 25 bytes
'^\<((t?\But)?-tut)+$'XXn
Try it online!
MATL appears to have \<
and \>
as its word boundary assertions, and apparently doesn't support \b
, yet somehow still supports \B
. I can't currently explain this; I thought it used PCRE.
Zsh, 35 34 bytes
[[ $1 =~ '^\<((t?\But)?-tut)+$' ]]
Try it online!
Zsh uses GNU ERE as its regex engine, so while \b
would work, \<
should be slightly more efficient due to only checking for the type of word boundary that can occur at that location.
PHP -F
, 48 47 46 bytes
<?=preg_match('/^\b((t?\But)?-tut)+$/',$argn);
Try it online! - bare-bones test harness
Try it online! - fancier test harness
t
in the first example? \$\endgroup\$tut-tutt-tut
for falsy :P \$\endgroup\$