Page 1 of 1

TextPipe Tip: Capturing text to a variable

Posted: Fri Jan 11, 2019 12:07 am
by dfhtextpipe
The Replace filter is very versatile.

Among the Action options when Perl pattern is used as the Find type are to Send variable N to subfilter
where N = 1 to 9 in the Actions dropdown.

This is the simplest method to use where the required subfilter is Capture text to a variable.
Here's an example:

Code: Select all

Restrict to each line in turn
|
|--Restrict to lines matching [<div type="book"]
|  |  [ ] Include line numbers
|  |  [ ] Include filename
|  |  [X] Match case
|  |  [ ] Count matches
|  |  Pattern type: 0
|  |  [X] UTF8 Support
|  |  [ ] Ignore empty matches
|  |  Context before: 0
|  |  Context after: 0
|  |
|  +--Perl pattern [<div type="book" osisID="(.+)"] and Send variable 1 to subfilter
|     |  [X] Match case
|     |  [ ] Whole words only
|     |  [ ] Case sensitive replace
|     |  [ ] Prompt on replace
|     |  [ ] Skip prompt if identical
|     |  [ ] First only
|     |  [ ] Extract matches
|     |      Maximum text buffer size 4096
|     |  [ ] Maximum match (greedy)
|     |  [ ] Allow comments
|     |  [ ] '.' matches newline
|     |  [X] UTF-8 Support
|     |
|     +--Capture text to variable @book
|         Reset: 0
|         Break: 

    
In this example the value of the osidID attribute is captured to the variable called book and may be used subsequently as @book@.

I hope you like this tip.

Re: TextPipe Tip: Capturing text to a variable

Posted: Fri Jan 11, 2019 12:13 am
by dfhtextpipe
Given that an underlying requirement for capturing text to a variable is to be under a Restrict to each line in turn,
here's another useful tip.

Suppose you wish to use the variable in a multiline part of the file being processed?

All you have to do beforehand is to linearize those sections of the file by (e.g.) replacing \n by \t.

Afterwards, it's simple enough to restore the original line layout by replacing all \t by \n.

The tip assumes EOLs are Linux style and that the input file doesn't already make use of horizontal tabs.

Enjoy the tip!

Re: TextPipe Tip: Capturing text to a variable

Posted: Mon May 06, 2019 1:48 pm
by DataMystic Support
Thanks David - please keep them coming!