0

I try to come up with regular expression patter that fulfills such requirements.

  1. it is US phone number format wit 3 groups
  2. I have input strings like this (999) 98__-9999 here there is extra _ at the end of second section which I want to delete

(999) 9_8_-9999 here there is extra _ at the end of second section I want to delete

(999) 9_-9999 here if second group length is < 3 and ends with _ there should be added _ to pad second group to 9__ (3 characters)

(999) 98-9999 here if second group length is equal to 3 or it ends with digit there shouldn't be any modifications

To sum up: If secondGroup.length > 3 && secondGroup.lastCharacter == '_' I want to remove this last character else if secondGroup.length < 3 && secondGroup.lastCharacter == '_' I wan to append "_" (or pad wit underscore to have 3 characters in total) else leave second group as in the input string.

The same should be applied to first group. The difference are the different delimiters i.e. (xxx) in first group and \sxxx- in second group

Here is my Swift code I have used to achieve it in brute force way by manually manipulating the string: (length 4 instead of 3 takes into account first delimiter like ( or \s. )

var componentText = ""
        let idx1 = newText.index(of: "(")
        let idx2 = newText.index(of: ")")
        if let idx1 = idx1, let idx2 = idx2 {
            var component0 = newText[..<idx1]
            var component1 = newText[idx1..<idx2]
            if component1.count  > 4 && component1.last == "_" {
                component1.popLast()
            } else if component1.count < 4 && component1.last == "_" {
                component1.append("_")
            }
            componentText += "\(component0)\(component1))"
        } else {
            componentText = newText
        }
        let idx3 = newText.index(of: " ")
        let idx4 = newText.index(of: "-")
        if let idx2 = idx2, let idx3 = idx3, let idx4 = idx4 {
            var component2 = newText[idx2..<idx3]
            component2.popFirst()
            var component3 = newText[idx3..<idx4]
            var component4 = newText[idx4...]
            if component3.count > 4 && component3.last == "_" {
                component3.popLast()
            } else if component3.count < 4 && component3.last == "_" {
                component3.append("_")
            }
            componentText += "\(component2) \(component3)-\(component4)"
        } else {
            componentText = newText
        }
        newText = componentText != "" ? componentText : newText

I think that using regular expression this code could be more flexible and much shorter.

Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • "I think that using regular expression this code could be more flexible and much shorter." -- I don't think so. Read [this](https://softwareengineering.stackexchange.com/questions/113237/when-you-should-not-use-regular-expressions). – user202729 Mar 04 '18 at 07:58
  • If the code is currently working you may want to post on [codereview.se] to see if they suggest using regex. – user202729 Mar 04 '18 at 07:59
  • [Example code that is worse with regex](https://stackoverflow.com/questions/2795065/how-to-determine-if-a-number-is-a-prime-with-regex). – user202729 Mar 04 '18 at 08:00
  • Your replacement logic is fairly complex, and I would rather just see some sample input and output data which covers the cases. – Tim Biegeleisen Mar 04 '18 at 08:03
  • It is not so complex. There are 3 groups each one consisting od digits and underscore. I need to modify 1st group and 2nd group. There are 2 reasons to modify each group (in both groups there are the same 2 cases). 1.) when group length > 3 and it ends with underscore then pop last underscore, 2.) when group length < 3 and it ends with underscore then append underscore at the end, in all other cases leave group as in input – Michał Ziobro Mar 04 '18 at 08:12
  • The third example contains ***2*** digits in the second part. Is it OK? Try [`(\)\s*\d(_))\b|(\)\s*\d[\d_]{2})_\b`](https://regex101.com/r/ODVcPn/1) to replace with `$1$2$3` – Wiktor Stribiżew Mar 07 '18 at 20:42

0 Answers0