Skip to content

Instantly share code, notes, and snippets.

@m-dekorte
Created April 20, 2025 12:32
Show Gist options
  • Save m-dekorte/7d6ec3e6e5c9ac96e2d85325e1f16e95 to your computer and use it in GitHub Desktop.
Save m-dekorte/7d6ec3e6e5c9ac96e2d85325e1f16e95 to your computer and use it in GitHub Desktop.
extractCharactersByCase M function | Extract only letters of a specified case. Non‑letter characters (digits, punctuation, whitespace) are dropped.
let
extractCharactersByCase = (input as nullable text, optional caseType as text) as text =>
Text.Combine(
List.Select(
Text.ToList(input ?? ""),
(s) =>
if Text.Proper(caseType ?? "Upper") = "Upper"
then not (Text.Lower(s) = s)
else not (Text.Upper(s) = s)
)
),
extractCharactersByCaseType = type function (
input as (type nullable text meta [
Documentation.FieldCaption = "Input Text",
Documentation.FieldDescription = "Text to extract characters from. Null treated as empty.",
Documentation.SampleValues = {"Hello World!", null}
]),
optional caseType as (type text meta [
Documentation.FieldCaption = "Case Type",
Documentation.FieldDescription = "“Upper” extracts uppercase letters; “Lower” extracts lowercase.",
Documentation.AllowedValues = {"Upper", "Lower"},
Documentation.SampleValues = {"Upper", "Lower"}
])
) as text meta [
Documentation.Name = "extractCharactersByCase",
Documentation.LongDescription = "Returns only characters of the specified case (uppercase or lowercase) from the input. Non‑letters are removed.",
Documentation.Category = "Text",
Documentation.Version = "1.00: Initial Version",
Documentation.Author = " Melissa de Korte",
Documentation.Examples = {
[
Description = "Default (uppercase)",
Code = "extractCharactersByCase(""Hello World!"")",
Result = "HW"
],
[
Description = "Lowercase extraction",
Code = "extractCharactersByCase(""Hello World!"", ""Lower"")",
Result = "elloorld"
]
}
]
in
Value.ReplaceType(extractCharactersByCase, extractCharactersByCaseType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment