ios - Swift: Regular expression to detect non alpha numeric characters -
i'm trying build regular expression detect string containing alphanumeric characters , special characters haven't found right it. here code:
var str = "asda~dd.asd98asd09asd098asd098ads908" let myregex = "^[a-z]{7}\\.[a-z0-9]{28}$" if (str.rangeofstring(myregex,options: .regularexpressionsearch) != nil) { str = "itwork.yes" }
any of knows how can build regular expression detect non alphanumeric characters ?
i'll appreciate help.
how using rangeofcharacter
?
// choose character set , add characters want check besides alphanumeric characters let set = characterset.alphanumerics.union(characterset(charactersin: ".\\")) // check range of characters in string let range = str.rangeofcharacter(from: set, options: .regularexpression)
that's 1 of cleanest ways doing this.
edit:
if want detect non-alphanumeric characters, use set.inverse
instead of set
when calling range function.
Comments
Post a Comment