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.strea...
New
Aetherus
This topic is about the Advent of Code 2021 - Day 4. Thanks to @bjorng , we now have a new Private Leaderboard. The entry code is: 370...
New
sb8244
Note: This topic is to talk about Day 10 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
coen.bakker
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
code-shoily
Here’s my day 3 code https://github.com/code-shoily/advent_of_code/blob/master/lib/2024/day_03.ex This was quite easy. I was afraid Par...
New
bjorng
Note: This topic is to talk about Day 5 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
cblavier
Hi, there :wave: Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New
bjorng
Note: This topic is to talk about Day 13 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
stevensonmt
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
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

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement