using System.Collections.Generic;
using System.Text.RegularExpressions;
public static void Main()
var testCases = new List<string>
@"*D$$$ADH**D$$$7ZJ**D$$$7ZI*",
@"*D$$$ADH* *D$$$7ZJ* *D$$$7ZI*",
@"$$29N6 *D$$$7ZJ* *D$$$7ZI*",
@"$$29N6 JKKKKKKKKKKKKHJH sdfsdfsf *ASDFER* *D$$$7ZJ* *D$$$7ZI*",
foreach (var test in testCases)
string result = CleanUpScanInput(test);
Console.WriteLine("Wejście:");
if (string.Equals(test.TrimEnd(), result.TrimEnd(), StringComparison.Ordinal))
Console.WriteLine(" -- Wynik bez zmian -- ");
Console.WriteLine("Wynik:");
Console.WriteLine(result);
Console.WriteLine("-------------------");
private static string CleanUpScanInput(string source)
.RemoveMultipleWhiteSpaces()
.Select(i => i.Trim().Trim(new[] { '*' }))
.Select(i => 6 <= i.Length && i.Length <= 7 && i.All(j => char.IsUpper(j) || char.IsDigit(j) || j == '$') ? $"*{i}*" : i)
.Join(Environment.NewLine);
return result + Environment.NewLine;
public static class StringExtensions
private static readonly Regex _multipleSpaces = new Regex(@" {2,}", RegexOptions.Compiled);
public static string CondenseSpaces(this string s)
return s.Aggregate(new StringBuilder(), (acc, c) =>
if (!char.IsWhiteSpace(c) || c == '\r' || c == '\n' || acc.Length == 0 || acc[acc.Length - 1] != ' ')
public static bool HasValue(this string value) => !string.IsNullOrWhiteSpace(value);
public static bool IsNullOrWhiteSpace(this string value) => string.IsNullOrWhiteSpace(value);
public static string Join(this IEnumerable<string> @values, string separator) => string.Join(separator, @values);
public static string RemoveMultipleNewLines(this string value) => value.Split(new[] { "\r\n" }, StringSplitOptions.None).Where(l => !l.IsNullOrWhiteSpace()).Join("\r\n");
public static string RemoveMultipleWhiteSpaces(this string value) => _multipleSpaces.Replace(value, " ");
public static string RemoveCharacters(this string value, Func<char, bool> predicate) => value is null ? null : new string(value.Where(c => !predicate(c)).ToArray());