user2924482
user2924482

Reputation: 9120

Swift: Replacing substring using regular expression not working

I'm building a regular expression to detect if match the following string but its not working. Here is my code:

var str = "asdasdd.ASD98ASD09ASD098ASD098ADS908"
let commsRegex = "\\D[a-z]{7,}.[^a-zA-Z0-9]{28,}"

if (str.rangeOfString(commsRegex,options: .RegularExpressionSearch) != nil) {

         str = "itwork.yes"   
        }

Any of you knows what I'm doing wrong?

I'll really appreciate your help

Upvotes: 0

Views: 156

Answers (1)

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26048

Your reg-ex appears to be incorrect, particularly this part:

[ ^ a-zA-Z0-9]

Notice the bolded ^, this means match only characters not in a-z A-Z and 0-9. I suspect you want to remove that character from your regex.

Also, I'm not sure about the \\D, this will match any non-digit, but your string starts with 7 characters which seems to match the next part, perhaps that should be removed as well if you expect that string to match the regex.

Upvotes: 2

Related Questions