2

I'm using the following code to find a substring using regex in Swift:

    let searchString = "Please contact us at <a href=\"tel:8882223434\" ><font color=\"#003871\"><b> 888.222.3434 </b></font></a> for assistance"
    let regexp = "^\\d+(\\.\\d+)*$"
    if let range = searchString.range(of:regexp, options: .regularExpression) {
        let result = searchString.substring(with:range)
        print(result) // <---- not printing the resulting string
    }

The desired output is 888.222.3434

Any help would be greatly appreciated. Thank you.

0

1 Answer 1

2

Remove the anchors and replace * with +.

let regexp = "\\d+(\\.\\d+)+"

Details

  • \d+ - 1 or more digits
  • (\\.\\d+)+ - one or more sequences of:
    • \. - a dot
    • \d+ - 1 or more digits.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your answer worked out perfectly for my need!

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.