locked
Split string for two different variants RRS feed

  • Question

  • Hi, I need some help with a regular expression.

    I have a CSV file with a column that holds the Contact Person Name & Tellephone Number (in one Column). I need to split these into two seperate values. The Field String can be in 2 formats ex: "ContactPersonName (000) 123 4567" or "ContactPersonName 0001234567".

    Can someone please help me with a Regex to split this string into 2? it should split on the first occurance of "(" or the first occurange of a number.

    Thanks
    Monday, February 9, 2009 8:14 AM

All replies

  • Hi,

    Try this: ([A-Za-z]+[ ]?[A-Za-z]+)[ ]*([0-9\(\) ]+)

    Then You will get MatchCollection with 3 groups. group nr 1 and 2 (zero indexed) is Your data.
    I have added possibility of space character in contact name, just in the case.

    Best regards, Rafal Ziolkowski http://ziuek.blogspot.com
    Monday, February 9, 2009 12:43 PM
  • Hi!

     

    Please check this pattern as well:

    (?i)[a-z]+\s(\d{10}|\(\d{3}\)\s\d{3}\s\d{4})

    Tuesday, December 28, 2010 2:21 PM
  • Will the name have -'s or .'s?

    Adam


    Ctrl+Z
    Monday, January 10, 2011 3:50 PM
  •  

    Hi,

    Please visit the this link http://www.regexlib.com/ . I hope this will helps you.

    Thanks

    Shiva.G

     

     

    Friday, January 14, 2011 7:05 AM
  • Could you please provide some example from column in your cvs file?
    Thanks, If you find this post as answer then click on "Mark it as answer"
    Tuesday, August 9, 2011 3:54 PM
  • Hi!

    And what about this one:

    (?i)([a-z]+)\s+(\(\d{3}\)[\d\s]+|\d{10})

     

    ([a-z]+)                   = Group 1 will contain ContactPersonName

    \(\d{3}\)[\d\s]+       = Group 2 will contain ContactInformation  [ this format ----> (000) 123 4567 ]

    \d{10}                     = Group 2 will contain ContactInformation  [ this format ----> 0001234567 ]

     

    1. ContactPersonName
    2. 0001234567

    OR

    1. ContactPersonName
    2. (000) 123 4567


    • Proposed as answer by NeverHopeless Saturday, August 20, 2011 8:42 PM
    Tuesday, August 9, 2011 8:29 PM