KimberlyBrooke

KimberlyBrooke

Trying to understand Exercism’s Acronym solution

Elixir Exercism Acronym

I am working to understand xianrusso’s solution to Exercism’s Acronym solution.

defmodule Acronym do
  def abbreviate(string) do
    Regex.scan(~r/(?<![A-Z])[A-Z]|(?<!\')\b[a-z]/, string)
    |> List.flatten
    |> Enum.map(&String.upcase/1)
    |> Enum.join
  end
end

The portion that I am working to digest is the Regex.

I believe this link contains the answer I just want to confirm that what I am thinking is happening is indeed what is happening. Advanced Regex Tutorial—Regex Syntax

I believe that in this ~r/(?<![A-Z])[A-Z]/ - that the (?<![A-Z]) is saying that as long as where we are at in the string does not have a capital letter that immediately precedes it then we can continue to keep matching it to [A-Z].

I believe that in this ~r/(?<!')\b[a-z]/ it is saying that as long as where we are at in the string there is not a ' that precedes it then we can keep matching it to \b[a-z]

Marked As Solved

eksperimental

eksperimental

The regex commented out in plain English:

regex =  ~r/
  (?<![A-Z])[A-Z]  # Match ONE uppercase letter from A to Z,
                   # that is not preceded by another uppercase letter from A to Z.

  |                # OR

  (?<!\')        # no need to escape the single quote  
  \b
  [a-z]          # Match ONE lowercase letter from a to z,
                   # that is preceded by a word boundary (\b)
                   # and that is not preceded by a single quote.
                   # Note: the beginning of a string is considered a word boundary.
  /x               # "x" modifier to to allow comments

Note the regex is valid, feel free to use it in your code, if you want.

Also Liked

eksperimental

eksperimental

quiet, but note that \b is s zero-width (same as lookbehinds) assertion, which is a word boundary. A limit between \w (a word) and a non-word (\W), or the beginning or end of a word.
So the second part of the regex is telling you [a-z] preceeded by any non-word that is not a single quote, and it can match a [a-z] at the beginning of the string.

KimberlyBrooke

KimberlyBrooke

Thank you!

eksperimental

eksperimental

No worries

Where Next?

Popular in Challenges Top

bismark
Took me a minute to remember my binary math :smile: :grimacing:… import Bitwise __DIR__ |&gt; Path.join("puzzle.txt") |&gt; File.stream...
New
sasajuric
Note: This topic is to talk about Day 12 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics...
New
adamu
Nobody’s doing Advent of Code this year? :smile: I might do the first week or so. For Day 1, first I solved it using regular expression...
New
bjorng
Note: This topic is to talk about Day 16 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
igorb
I found today a bit tedious: advent-of-code-2024/lib/advent_of_code2024/day15.ex at main · ibarakaiev/advent-of-code-2024 · GitHub.
New
bjorng
This topic is about Day 14 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
stevensonmt
Reasonably pleased with my solution. The bitstring packet problems are so well suited to Erlang/Elixir it’s almost not fair. defmodule D...
New
seeplusplus
Hello all, hopefully I post this before someone else does and I don’t dupe. IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
New
Aetherus
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby. Anyway, here is my solution:
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement