clem

clem

How to validate full string with Regex

I currently developing a rest API with elixir and plug.

I will like to validate user input to accept only letters, but when I use the regex, it only validates a letter, not the full string.

Example:
String.match?(“A”, ~r/[1]$/i) returns true, but when I try String.match?(“foo”, ~r/[2]$/i) returns false.

Any help, please?


  1. a-z ↩︎

  2. a-z ↩︎

First Post!

NobbZ

NobbZ

The regex asks for a single lowercase letter from the Latin alphabet. If you want to have at least one of those and do not care for an upper bound you need to use the + quantifier: ~r/^[a-z]+$/.

Most Liked

NobbZ

NobbZ

Letters and numbers is broad… I consider ä a letter, do you? What’s about Chinese scripts? Or kyrillic? Greek? Persian numbers? Do you consider those as valid?

If you restrict to Arabic numerals and Latin letters in upper and lowercase it’s something like this:

~r/^[a-zA-Z0-9]+$/
zachgarwood

zachgarwood

If you don’t mind underscores in there as well, you could use the \w “word characters” class, for example:

~r/^\w+$/

The \w class is equivalent to [A-Za-z0-9_].

Last Post!

clem

clem

Thank you

Where Next?

Popular in Discussions Top

mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement