Determine if string contains regex characters

Hi,

Overview

I would like to know if there is a way to determine if the string contain regex characters. (Just want to know true or false).

Questions

  1. What is the proper way to determine if a string contains regex characters?
  2. Does Regex / RegexComponent help in any way to achieve this?
  3. Or is there any other work around?

Code

let searchText = "a(.*)b"
do {
    let regex = try Regex(searchText)
    //Is there a way to determine if searchText contains any regex characters?
    //Or should I just check if they contain the characters ., +, *, ?, ^, $, (, ), [, ], {, }, |, \
} catch {
    print("Error: \(error)")
}
1 Like