return only Digits 0-9 from a String

后端 未结 8 1909
遇见更好的自我
遇见更好的自我 2020-11-27 04:53

I need a regular expression that I can use in VBScript and .NET that will return only the numbers that are found in a string.

For Example any of the following \"str

8条回答
  •  清酒与你
    2020-11-27 05:04

    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.

提交回复
热议问题