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
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
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
Thank you!
eksperimental
No worries
Popular in Challenges
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









