0

Consider the below given is a string

PO BOX number 1234, CA - 3265 TEL: +1-200-200-2000 FAX: +1-200-200-2001
PO BOX number 1234, CA - 3265 Service: +1-200-200-2002 \ +1-200-200-2022 F : +1-200-200-2003
PO BOX number 1234, CA - 3265 care: +1-200-200-2004 f : +1-200-200-2005
PO BOX number 1234, CA - 3265 fire: +1-200-200-2006 fax : +1-200-200-2007
PO BOX number 1234, CA - 3265 help: +1-200-200-2008 fax - +1-200-200-2009
PO BOX number 1234, CA - 3265 fax : (+123)-4567890 \ (+123)-4567891

The regular expression mentioned below is the one I'm using for finding a fax number in different representation

(?:(f|F)((a|A)(X|x))?(?:\s?(\-|\:)?\s?))(?:\s?[0-9\+\.\(\)\/\\\-]\/?\\?\s?){7,30}

Ex: The above regex would match all the Fax from the given string as shown below

FAX: +1-200-200-2001
F : +1-200-200-2003
f : +1-200-200-2005
fax : +1-200-200-2007
fax - +1-200-200-2009
fax : (+123)-4567890 \ (+123)-4567891

Note: I am considering the phone numbers as rest of the numbers available in the given text apart from taking the FAX.
Ex: TEL: +1-200-200-2000
Service: +1-200-200-2002 \ +1-200-200-2022
care: +1-200-200-2004
fire: +1-200-200-2006
help: +1-200-200-2008

So I need the phone numbers whose text not starts with [faxFAX]. I have tried with [^faxFAX] just changing the above specified regular expression but I couldn't get the desired result.please help me out to write a regular expression for this.

Vinodh Velumayil
  • 742
  • 3
  • 8
  • 24
  • I can use this `[bcdeghijklmnopqrstuvwyzBCDEGHIJKLMNOPQRSTUVWYZ]+` but it'll neglect if the text before the number ends with **f|a|x|F|A|X** – Vinodh Velumayil Jun 13 '15 at 10:36

1 Answers1

1

You can use (*SKIP)(*FAIL):

(?:(f|F)((a|A)(X|x))?(?:\s?(\-|\:)?\s?))(?:\s?[0-9\+\.\(\)\/\\\-]\/?\\?\s?){7,30}(*SKIP)(*FAIL)|(?:\s?[0-9\+\.\(\)\/\\\-]\/?\\?\s?){7,30}

See DEMO

Edit: Explanation of (*SKIP)(*FAIL} as suggested in the comments.

Community
  • 1
  • 1
karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • [Explanation of SKIP and FAIL](http://stackoverflow.com/questions/24534782/how-do-skip-or-f-work-on-regex). A brief summary in the answer itself would also be good. – Bernhard Barker Jun 13 '15 at 11:43
  • @karthikmanchala hi I have some problem with the regex you gave, when I utilized that and tried in [http://www.regexr.com] and of course it gives me the same, when I was using the same regex in my code. – Vinodh Velumayil Jun 15 '15 at 10:32
  • @VinodhVelumayil `regexr` uses javascript regex engine.. and (*SKIP) (*FAIL) are not supported in javascript.. but in PCRE, PHP.. are you using javascript?. – karthik manchala Jun 15 '15 at 10:36
  • @karthikmanchala yes exactly I have told you that I am using the language **R** so it gets me the same output like try that regex with removing (star) from SKIP and FAIL – Vinodh Velumayil Jun 15 '15 at 10:40
  • @VinodhVelumayil `(*SKIP) (*FAIL)` works in R.. are you sure it is not because of other issues? – karthik manchala Jun 15 '15 at 11:25
  • @karthikmanchala yep I have verified it. It doesn't works well, as it is taking the fax along with the expected result. – Vinodh Velumayil Jun 15 '15 at 12:58