SoFunction
Updated on 2025-04-11

Custom regular expression operator in swift = ~ Detailed explanation

What is a regular expression?

Regular expression, also known as regular expression, regular expression (English: Regular expression, often abbreviated as regex, regexp or RE in code), is a concept of computer science. Regular expressions use a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are usually used to retrieve and replace text that conforms to a certain pattern.

Swift has not supported regular expressions at the language level so far, and there may not be many scenarios for regular expressions to be used when developing apps.

Package

In Cocoa, we can use NSRegularExpression to do regular matching, so we encapsulate a RegularExpHelper to match whether a string meets a certain regular expression based on NSRegularExpression.

struct RegularExpHelper {
 let RegularExp: NSRegularExpression 
 init(_ pattern: String) throws {
  try RegularExp = NSRegularExpression(pattern: pattern, options: .caseInsensitive)
 } 
 func match(inpuut: String) -> Bool {
  let matches = (in: inpuut, options: [], range: NSMakeRange(0, ))
  return  > 0
 }
}

Custom =~

With the encapsulated RegularExpHelper, we can easily customize operators.

infix operator =~ : ATPrecedence
precedencegroup ATPrecedence {
 associativity: none
 higherThan: AdditionPrecedence
 lowerThan: MultiplicationPrecedence
}
func =~ (input: String, RegularExp: String) -> Bool {
 do {
  return try RegularExpHelper(RegularExp).match(inpuut: input)
 } catch _ {
  return false
 }
}

Operator definition

  • infix means defining a median operator (before and after all inputs)
  • prefix means defining a prefix operator (previously input)
  • postfix means defining a post-bit operator (after input)

associativity law

That is, the calculation order when multiple operators of the same kind appear

  • left (order from left to right)
  • right (order from right to left)
  • none (the default is none, and it will not be combined again)

Priority

  • higherThan priority is higher than AdditionPrecedence This is the type of addition
  • lowerThan Priority is lower than MultiplicationPrecedence

Then we can use it

 if "88888888@" =~ "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" {
  print("Compare the Email Rules")
 } else {
  print("Not compliant with email rules")
 }

Notice

  • Pay attention to the use of escape characters when using regular expression strings.
  • The operator of swift cannot be defined in the local domain because the operator needs to be used globally.
  • There are many risks in overloading and custom operators. Please ask yourself more if you really need to do this before using it!

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.