25

I define a string with:

static let Regex_studio_tel = "^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$"

But there comes an issue:

Invalid escape sequence in literal

The picture I token:

enter image description here


Edit -1

My requirement is match special plane numbers use Regex, such as:

My company have a special plane number:

028-65636688 or 85317778-8007  

// aaa-bbbbbbbb-ccc we know the aaa is the prefix, and it means City Dialing Code, and bbbbbbbb is the main tel number, cccc is the landline telephone's extension number,

such as my company's landline telephone is 028-65636688, maybe our company have 10 extension number: 028-65636688-8007 ,028-65636688-8006,028-65636688-8005 and so on. Of course, it maybe have a ext-number at the end.

028-65636688-2559
aircraft
  • 25,146
  • 28
  • 91
  • 166
  • 1
    TRY : static let Regex_studio_tel = "^(0[0-9]{2,3}'\'-)?([2-9][0-9]{6,7})+('\'-[0-9]{1,4})?$" – Kirit Modi Dec 28 '16 at 07:17
  • You may need some more examples to clarify your requirement. `65636688` is not valid, neither `028-65636688-85317778-8007` is not, OK? – OOPer Dec 28 '16 at 08:46

2 Answers2

60

Two character sequence \ - is not a valid escape sequence in Swift String. When you need to pass \ - to NSRegularExpression as pattern, you need to write \\- in Swift String literal.

So, your line should be something like this:

static let Regex_studio_tel = "^(0[0-9]{2,3}\\-)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?$"

ADDITION

As Rob commented, minus sign is not a special character in regex when appearing outside of [ ], so you can write it as:

static let Regex_studio_tel = "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$"
OOPer
  • 47,149
  • 6
  • 107
  • 142
3

I'm guessing that your intent was to escape the - characters. But that's not necessary (and is incorrect). If your intent was to match just dashes, you should remove those backslashes entirely:

let pattern = "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$"

Unrelated, but I'm suspicious of that + character. Did you really mean that you wanted to match one or more occurrences of [2-9][0-9]{6,7}? Or did you want to match exactly one occurrence?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • thanks bro, I want to match the landline number, and I posted is not available in swift, match all of the landline number's will also consider `+`? – aircraft Dec 28 '16 at 08:10
  • I don't know what format these landline numbers you want to match are. But I'm pretty sure it's not multiple sequences of `[2-9][0-0]{6,7}`. For example, your regex will match a sequence of the number 9 repeated 700 times. I can't believe that's what you intended. Maybe you can edit your question and show us actual examples of all of the sorts of strings you want to match. – Rob Dec 28 '16 at 08:23
  • @aircraft - OK, I see your updated your question. So, are you trying to match the full comma separated list of numbers, or do you want a regex that you can use to iterate through the numbers within that broader string? – Rob Dec 28 '16 at 18:34