Subscribe Bookmark RSS Feed

Obtaining substrings with variable number of characters

bhagbom

Community Trekker

Joined:

Jun 23, 2011

I have a set of file names that contain within the name an ID number. These ID numbers do not necessarily have the same character length. Any ideas on how to achieve this? This is what I have for fixed ID character length.

For (k =1,
k < N Items( listOfFiles ),
k++,
IDNumber = subsr( listOfFiles, 1, 8 );
insertinto( listOfIDNumbers, IDNumber );
);

Issue arises when ID number is not 7 characters in length and I have complex filenames.
ABCDE_1234567_ABCD.csv
ABC_123_ABCDEFG.csv

What I want is to just get 1234567 and 123. Can I key off .csv somehow or perhaps underscore?
4 REPLIES
mpb

Super User

Joined:

Jun 23, 2011

Try this:

IDNumber = Word( 1, listOfFiles, "_" )
bhagbom

Community Trekker

Joined:

Jun 23, 2011

That works great, thanks for the help!
mewing

Community Trekker

Joined:

Jun 23, 2011

The other option would be to leverage regular expressions. If the file names did not have _'s separating the various portions but the ID name was uniquely composed of numbers and the rest of the file name did not contain numbers then this would work.

A regex syntax guide that I always reference is:
http://www.regular-expressions.info/reference.html

Fixed a stupid grammar mistake


Message was edited by: mewing
mpb

Super User

Joined:

Jun 23, 2011

Good point about regular expressions. For the record, in the case you described, the following would also work:

IDNumber = Word( 1, listOfFiles, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" )