starskie Posted October 20, 2010 Share Posted October 20, 2010 Let me explain what I am trying to do. I'm trying to extract all phone numbers from a form of data, and it should result in an array of all phone numbers within the text. So var I managed absolutely nothing, I've been trying to write a regex pattern for this for quite a while and can not seem to make anything work. I'm reading off of a cheat sheet as I do not know so much about regex in total, except for finding simple things. What I currently have uttered up is the following expression: /\+*(?=[1-999])|(!?=0){7,9}[0-9]/ It doesn't even find anything, AT ALL. I'm testing with the following string: asd+27845513786 asd +98765433 asd It should return the two phone numbers, but doesn't. The rules I've laid out for it are: 1. Look for the international symbol, indicating it's an international dialing number with a valid extension(from +1 to +999). 2. If the plus symbol is present, make sure the next following character is a number. 3. If there is none, look at the length to validate it is between 7 and 10 digits long. 4. In the event that the number is divided (correctly via international standers) by either a hyphen(-) or space make sure the amount of digits in between them are either 3 or 4. It sounds so simple, but my methodologies have not succeeded thus var. If someone could give me advice on this matter it would greatly be appreciated. Quote Link to comment Share on other sites More sharing options...
carpiediem Posted October 20, 2010 Share Posted October 20, 2010 This will match you example, but it will also accept numbers with as few as 6 digits or as many as 12. Hope it helps. /\+(?:\d{3,4}[-\s]?){2,3}/ Quote Link to comment Share on other sites More sharing options...
starskie Posted October 20, 2010 Author Share Posted October 20, 2010 Thanks for your help, although it's not exactly what I need. I a large set of data it just wouldn't work. I've been playing around with it some more, and actually have something that works but not as I hoped. It is getting the phone numbers correctly, international and local ones but it doesn't pick up hyphens or spaces. I think I'm just making life harder for myself. I've now decided to maybe run two regex searches. one to pick up international phone numbers and one for local ones. Just one thing with the international ones, it is not including the + symbol in the results: why is that? International search: \+(?=[1-999])(\d{4}[0-9][-\s]\d{3}[0-9][-\s]\d{4}[0-9])|(\d{7,11}[0-9]) International search shorter(includes the symbol): \+(?=[1-999])\d{7,11} local: \d{7,10}[0-9] Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.