0

I am trying to use Regex expressions but this does not work and I don't understand why. The println is not showing up in the output. I followed this tutorial to use this class:

class Regex {
  let internalExpression: NSRegularExpression
  let pattern: String

  init(_ pattern: String) {
    self.pattern = pattern
    var error: NSError?
    self.internalExpression = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)
  }

  func test(input: String) -> Bool {
    let matches = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input)))
    return matches.count > 0
  }
}

And this other tutorial to create the Regex.

The code returns the text when I do not use Regex. If I do use it, the println does not appear. I tried "(/([a-z]+)|([0-9]+)/ig)$" and "/([a-z]+)|([0-9]+)/ig$" but none of them work.

func textFieldDidEndEditing(textField: UITextField) {
        println("end editing")

        if let match = textField.text.rangeOfString("/([a-z]+)|([0-9]+)/ig$", options: .RegularExpressionSearch) {
            println("\(match) is probably f**ked off and left as a f**king bastard")
        }

        if Regex("/([a-z]+)|([0-9]+)/ig").test(textField.text){
            println("yes")
            textField.text = textField.text.uppercaseString
        }
    }
4
  • What is your input for textField.text? Could you give us some examples of what you want to match the regex, and what you don't want to match? Commented Mar 1, 2015 at 19:15
  • I get a compiler error in above self.internalExpression =... : not unwrapped. Commented Mar 1, 2015 at 19:23
  • @Thomas Kilian You should add a ! at the end of the line to unwrap Optional value. See the 'Implicitly Unwrapped Optionals' section in The Swift Programming Language self.internalExpression = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)! Commented Mar 1, 2015 at 19:25
  • @devxoul Thx for moving it as a comment, but my comment was just meant as a comment to the OP :-) I know that I have to add ! Commented Mar 1, 2015 at 21:47

1 Answer 1

3

In this case, you do not need to use (/ ..... /ig)$, so try this:

   if let match = textField.text.rangeOfString("([a-z]+)|([0-9]+)", options: .RegularExpressionSearch) {
                println("\(match) is bla bla bla...")
            }

It will be work and return the range of the first occurrence of a given string

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

3 Comments

thanks, what about case insensitive? Is there a better way than ( [a-z]+ | [A-Z]+ ) ?
If you mean the way to find uppercase simbols, then [A-Z] is the standard choice
anyway to still use i , g , u ... flags in pattern?

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.