Search code examples
phpregexstringtext-parsingarray-column

Parse lines of a text file where values are separated by a varying number of whitespace characters


I need to get the company name and its ticker symbol in different arrays. Here is my data which is stored in a txt file:

3M Company      MMM
99 Cents Only Stores    NDN
AO Smith Corporation    AOS
Aaron's, Inc.   AAN

and so on

How would I do this using regex or some other techniques?


Solution

  • Iterate over each line, and collect the data with a regular expression:

    ^(.+?)\s+([A-Z]+)$
    

    The backreference $1 will contain the company name, $2 will contain the ticker symbol.

    You can also split the string in two with a two or three-space delimiter and trim the resulting two strings. This only works if you are sure the company name and ticker symbol are always separated by enough spaces, and the company name itself doesn't contain that amount of spaces.