0

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

3
  • 1
    try like this "^[a-z]{7}\\.[A-Z0-9]{28}$" Commented Nov 17, 2016 at 1:46
  • 1
    @LeoDabus this works great. Question if I asdasdd to add~sdd doesn't work what can modify to able to detect "and~sdd.ASD98ASD09ASD098ASD098ADS908" for example Commented Nov 17, 2016 at 17:52
  • Is ~ required to be at an specific place (middle of that first 7 chars group)"[a-z]{3}~[a-z]{3}\\.[A-Z0-9]{28}$" if not "[a-z~]{7}\\.[A-Z0-9]{28}$" Commented Nov 17, 2016 at 23:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. Also, the failure to escape the . character (as shown by Leo) is also a problem. Also, I assume he included the ^ to try to match the start of the string, so it's not so much as "remove" the ^ as to just move it to the start of the pattern, before the [ and ].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.