Here might be one way. In your script choose a "Set variable" step (e.g. $$myFormattedOutput) if that is what you want, or set field (e.g. Table::c_myOutputField) and then a calculation such as this one...
_srcTxt = "Now is the winter of our discontent¶made glorious summer by this sun of York";
_underlineTxt = TextStyleAdd( "winter of our discontent"; Underline );
_underlineStartPos = Position ( _srcTxt ; _underlineTxt; 1 ; 1 );
_startTxt = Left( _srcTxt; _underlineStartPos -1 );
_underlineStopPos = _underlineStartPos + Length( _underlineTxt );
_endTxt = Right( _srcTxt; Length( _srcTxt ) - _underlineStopPos + 1 )
_startTxt & _underlineTxt & _endTxt
You would also want to think through the consequences of (and probably test for) what would happen if underlined term is not found in text (can always happen).
I'm assuming here that YourTextField contains ALWAYS 3 paragraphs.
Set Field [ YourTable::YourTextField ;
NoStyles = TextFormatRemove ( YourTable::YourTextField ) ;
FirstParagraph = GetValue ( NoStyles ; 1 ) ;
SecondParagraph = GetValue ( NoStyles ; 2 ) ;
ThirdParagraph = GetValue ( NoStyles ; 3 ) ;
Key = MiddleWords ( SecondParagraph ; 3 ; WordCount ( SecondParagraph ) )
Substitute ( FirstParagraph ; Key ; TextStyleAdd ( Key ; Underline ) ) & ¶ & SecondParagraph & ¶ & ThirdParagraph
Commit Records/Requests [ ]
Nice approach. If the whole thing literally exists in one single text field (as OP did indeed write), then one might want to consider parsing the already existing data just once using Ray's approach and storing the results (as well as all future entries) in separate fields. Wouldn't this in the long-term be a more flexible approach? One that would give you a chance to correct any problems (such as those arising from our assumptions about the number of line returns in the source text)?
Kevin and Ray,
Many thanks for your suggestions, that made me aware of the existence, nature and power of the 'Let' function, and that taught me how to use it.
I ended up creating a solution very similar to Ray's, solving not only my original issue but also a few others that I had not included in the original post for the sake of concision and clarity.
Again, I appreciate the time you took to help me. W.
Hey Wladdy, can you share what you came up with for the rest of the community to learn from please? =)