возвратите только Цифры 0-9 из Строки

NotificationCenter.default.addObserver(self, selector: #selector(addNewTask(_:)), name: 
NSNotification.Name.init("com.todolistapp.addtask"), object: nil)  //1

@objc func addNewTask(_ notification: NSNotification) {  //2

}

В строке 1 вы просто сообщаете NotificationCenter о том, что хотите прослушать уведомление определенного вида, и когда это уведомление будет запущено, вы хотите вызвать одну функцию. Вы рассказываете о методе, используя selector. Вы делаете селектор, используя имя функции.

Указанный вами метод не будет вызываться, когда вы находитесь в строке 1. Он будет вызван NotificationCenter, когда уведомление будет активировано. И тогда у вас фактически будет объект уведомления, который должен быть передан функции.

Это зависит от вас, хотите ли вы этот параметр или нет. Вы также можете опустить его, если хотите.

66
задан IAdapter 10 May 2009 в 05:47
поделиться

5 ответов

I don't know if VBScript has some kind of a "regular expression replace" function, but if it does, then you could do something like this pseudocode:

reg_replace(/\D+/g, '', your_string)

I don't know VBScript so I can't give you the exact code but this would remove anything that is not a number.

EDIT: Make sure to have the global flag (the "g" at the end of the regexp), otherwise it will only match the first non-number in your string.

12
ответ дан 24 November 2019 в 14:48
поделиться

Note: you've only solved half the problem here.

For US phone numbers entered "in the wild", you may have:

  • Phone numbers with or without the "1" prefix
  • Phone numbers with or without the area code
  • Phone numbers with extension numbers (if you blindly remove all non-digits, you'll miss the "x" or "Ext." or whatever also on the line).
  • Possibly, numbers encoded with mnemonic letters (800-BUY-THIS or whatever)

You'll need to add some smarts to your code to conform the resulting list of digits to a single standard that you actually search against in your database.

Some simple things you could do to fix this:

  • Before the RegEx removal of non-digits, see if there's an "x" in the string. If there is, chop everything off after it (will handle most versions of writing an extension number).

  • For any number with 10+ digits beginning with a "1", chop off the 1. It's not part of the area code, US area codes start in the 2xx range.

  • For any number still exceeding 10 digits, assume the remainder is an extension of some sort, and chop it off.

  • Do your database search using an "ends-with" pattern search (SELECT * FROM mytable WHERE phonenumber LIKE 'blah%'). This will handle sitations (although with the possibility of error) where the area code is not provided, but your database has the number with the area code.

6
ответ дан 24 November 2019 в 14:48
поделиться

В .NET вы могли извлекать только цифры из строки. Примерно так:

string justNumbers = new String(text.Where(Char.IsDigit).ToArray());
183
ответ дан 24 November 2019 в 14:48
поделиться

By the looks of things, your trying to catch any 10 digit phone number....

Why not do a string replace first of all on the text to remove any of the following characters.

<SPACE> , . ( ) - [ ] 

Then afterwards, you can just do a regex search for a 10 digit number.

\d{10}
1
ответ дан 24 November 2019 в 14:48
поделиться

Have you gone through the phone nr category on regexlib. Seems like quite a few do what you need.

0
ответ дан 24 November 2019 в 14:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: