Subscribe Bookmark RSS Feed

Extract string from name

Highlighted
Dipesh

New Contributor

Joined:

Jun 8, 2017

Hi, 

 

I would like to extract a string of data from a name. The position of the string varies in the name and the name has a variable number of sub-strings within it. An example is shown below:

 

(GPN) CI-4/E7/DH-1 15S-40 L1807 
DEO (GPN) JD CI-4/E7/DH-1 5S-40 L1807 

(GPN) JD CI-4/E7/DH-1 40 L1807 

 

I am interested in two parts: a string which contains the letter S (so in the examples, 15S-40 & 5S-40) AND if w is not present, I am interested in the numeric value. 

 

Is it possible to extract this using standard formula functions? 

 

Any help would be great. 

 

Thanks,

Dipesh 

1 ACCEPTED SOLUTION

Accepted Solutions
pauldeen

Community Trekker

Joined:

Oct 24, 2014

Solution

You haven't provided an example of something with a W in it so that is untested. I think this will solve your problems:

YourList = {"(GPN) CI-4/E7/DH-1 15S-40 L1807", "DEO (GPN) JD CI-4/E7/DH-1 5S-40 L1807", "(GPN) JD CI-4/E7/DH-1 40 L1807"};
Part1 = Part2 = {};
For(i=1,i<=N items(YourList),i++,
	SubWords = Words(YourList[i], " ");
	For(k=1,k<=N Items(SubWords),k++,
		If(contains(SubWords[k],"S")>0,
			Part1[i] = SubWords[k];
			k=N Items(SubWords)+1;
		)
	);
);
For(i=1,i<=N items(Part1),i++,
	If(contains(part1[i],"W")==0,
		Part2[i] = right(part1[i],2);
	)
);
4 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

It appears that the component you want to strip off is the second to the last grouping.  If that is the case, then the code below will do that.  The Word() function is a very powerful function.  It's documentation can be found in either the Scripting Guide or the Scripting Index

     Help==>Books==>Scripting Guide

     Help==>Scripting Index==>Word

Names Default To Here( 1 );
dt = New Table( "test",
	New Column( "String",
		character,
		values(
			{"(GPN) CI-4/E7/DH-1 15S-40 L1807", "DEO (GPN) JD CI-4/E7/DH-1 5S-40 L1807",
			"(GPN) JD CI-4/E7/DH-1 40 L1807"}
		)
	)
);
dt << New Column( "Value", character, formula( Word( -2, :String, " " ) ) );

 

Jim
Dipesh

New Contributor

Joined:

Jun 8, 2017

Hello,
Thanks for the reply.

Unfortunately there is no commonality where the word I would like to extract is in the string - sometimes it is position 2 sometimes it is position 7!
Regards,

Dipesh
thickey1

Community Trekker

Joined:

Jun 4, 2015

Using a Regular Expression you can match a 'space' followed by some characters until you hit an 'S-' then more characters until you hit the next 'space'

 

PAT = "(GPN) CI-4/E7/DH-1 15S-40 L1807";

x = regex(PAT,"\S+(?=S-).* ");
show(x);

pauldeen

Community Trekker

Joined:

Oct 24, 2014

Solution

You haven't provided an example of something with a W in it so that is untested. I think this will solve your problems:

YourList = {"(GPN) CI-4/E7/DH-1 15S-40 L1807", "DEO (GPN) JD CI-4/E7/DH-1 5S-40 L1807", "(GPN) JD CI-4/E7/DH-1 40 L1807"};
Part1 = Part2 = {};
For(i=1,i<=N items(YourList),i++,
	SubWords = Words(YourList[i], " ");
	For(k=1,k<=N Items(SubWords),k++,
		If(contains(SubWords[k],"S")>0,
			Part1[i] = SubWords[k];
			k=N Items(SubWords)+1;
		)
	);
);
For(i=1,i<=N items(Part1),i++,
	If(contains(part1[i],"W")==0,
		Part2[i] = right(part1[i],2);
	)
);