user2636197 user2636197 - 8 months ago 69
Swift Question

swift ios alpha numeric regex that allows underscores and dashes

I am using this lib for validation and are trying to add my own regex.

What I want to do is to make a regex that allows alphanumeric A-Z 0-9 together with dashes and unserscores -_

I have tryed

let regex = "[a-zA-Z0-9_-]"
but I cant get it to work.

I also want the regex to not only allow english letters, but all languishes.

The lib works cause I have made another regex that only allows ints 0-9 which works

let intRegex = "^[0-9]*$"


Your regex look good but it will only match a single character. Do this "^[a-zA-Z0-9_-]*$" instead to match more than one character.

breakup --

^ -- start of string

[\pL0-9_-] -- characters you want to allow

* -- any number of characters (the crucial bit you were missing)

$ -- end of string