1

I have a app which detects the text in any image user takes from the app or from existing image using OCR. This is working but if image has lots of unnesseasry numbers with the number set i want, everything will be detected. So i want to extract the numbers which always has a pattern from the detected text string,

As an example, my string can be something like this,

"3456 7834 5623 iuhef654sca 869878zsc ttv"

I want to get only the "3456 7834 5623" part from the above string. Which has two spaces, three sets of 4 digits. How did i do it? what is the best way to accomplish it?

Bishan
  • 401
  • 1
  • 9
  • 16

1 Answers1

0

You can use regex pattern "(\\d){4} (\\d){4} (\\d){4}" as follow:

let string = "3456 7834 5623 iuhef654sca 869878zsc ttv"
let pattern = "(\\d){4} (\\d){4} (\\d){4}"
if let range = string.range(of: pattern, options: .regularExpression) {
    print(string[range])  // "3456 7834 5623\n"
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571