jwahr
jwahr

Reputation: 25

Crystal Syntax - "Extract" Part of a String Within a String

I am doing this in i-net Designer (another Crystal Report type program).
My problem has two parts...

I am trying to "extract" part of a string from a string. The string that I need will always be in the middle of the main string. But will NEVER be in the same position. The Description and length will always change.

Below is an example.
Description (this is the main string I am "extracting" from):

    The Name of my Book Here CD CTN1234-5678-9 2-1 CD Carton

Part 1
The string I am trying to extract is the Part#, CTN1234-5678-9. This will always be 14 characters long(3 letters, 9 numbers and 2 dashes).

Part 2
I need the description portion only. This is the title of the book starting from the LEFT up to the Part#(CTN1234-5678-9). So I would need only The Name of my Book Here CD

Doing this in "Formula Editor" for my formula fields @PartNumber and @Description

Upvotes: 0

Views: 8902

Answers (3)

craig
craig

Reputation: 26262

You could use a regular expression:

// {@Book Title}
// ([A-Z]{3}[0-9]{4}-[0-9]{4}-[0-9]{1}) - matches the part #
// (.*?) - a non-greedy captured group
// (?= ) - a look-ahead
RegexMatch( "(.*?)(?=([A-Z]{3}[0-9]{4}-[0-9]{4}-[0-9]{1}))" )

You'll need the Crystal Reports Regex Library.

Upvotes: 0

jwahr
jwahr

Reputation: 25

For Part 2 - Description I came up with this (just modifying the last line of code):
Thanks again @Lee Tickett
Appreciate your help!

local stringVar myString := 'The Name of my Book Here CD CTN1234-5678-9 2-1 CD Carton';
local numberVar myPosition := length(myString);

while not(mid(myString, myPosition - 14, 14) like '???????-????-?') and myPosition > 14
 do myPosition := myPosition - 1;

uppercase(trim(left(myString, myPosition - 15)));

Upvotes: 0

Lee Tickett
Lee Tickett

Reputation: 6027

I'm sure there is a simpler way- but this works:

local stringVar myString   := 'The Name of my Book Here CD CTN1234-5678-9 2-1 CD Carton';
local numberVar myPosition := len(myString);

while not(mid(myString, myPosition - 14, 14) like '???????-????-?') and myPosition > 14
 do myPosition := myPosition - 1;

mid(myString, myPosition - 14, 14);

Upvotes: 1

Related Questions