A practical example showing how to remap a match found in a substring back to the global coordinates of the original document.
ID: 850
See: ApplyOffset
using uCalcSoftware;
var uc = new uCalc();
string document = "HEADER::BEGIN[data to find]END::FOOTER";
// 1. Isolate the content block we want to search in.
// Assume the block we care about is between BEGIN and END.
var startIndex = document.IndexOf("BEGIN[") + 6;
var endIndex = document.IndexOf("]END");
var contentLength = endIndex - startIndex;
var contentBlock = document.Substring(startIndex, contentLength);
Console.WriteLine($"Searching within substring: '{contentBlock}'");
// 2. Perform a find operation on just that substring.
var t = uc.NewTransformer().SetText(contentBlock);
t.Pattern("find");
t.Find();
var matches = t.Matches;
Console.WriteLine($"Local match StartPosition: {matches[0].StartPosition}"); // Relative to 'contentBlock'
// 3. Apply the offset to remap to the global 'document' coordinate space.
matches.ApplyOffset(startIndex);
Console.WriteLine($"Global match StartPosition: {matches[0].StartPosition}");
Searching within substring: 'data to find'
Local match StartPosition: 8
Global match StartPosition: 22 using uCalcSoftware; var uc = new uCalc(); string document = "HEADER::BEGIN[data to find]END::FOOTER"; // 1. Isolate the content block we want to search in. // Assume the block we care about is between BEGIN and END. var startIndex = document.IndexOf("BEGIN[") + 6; var endIndex = document.IndexOf("]END"); var contentLength = endIndex - startIndex; var contentBlock = document.Substring(startIndex, contentLength); Console.WriteLine($"Searching within substring: '{contentBlock}'"); // 2. Perform a find operation on just that substring. var t = uc.NewTransformer().SetText(contentBlock); t.Pattern("find"); t.Find(); var matches = t.Matches; Console.WriteLine($"Local match StartPosition: {matches[0].StartPosition}"); // Relative to 'contentBlock' // 3. Apply the offset to remap to the global 'document' coordinate space. matches.ApplyOffset(startIndex); Console.WriteLine($"Global match StartPosition: {matches[0].StartPosition}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
string document = "HEADER::BEGIN[data to find]END::FOOTER";
// 1. Isolate the content block we want to search in.
// Assume the block we care about is between BEGIN and END.
auto startIndex = document.find("BEGIN[") + 6;
auto endIndex = document.find("]END");
auto contentLength = endIndex - startIndex;
auto contentBlock = document.substr(startIndex, contentLength);
cout << "Searching within substring: '" << contentBlock << "'" << endl;
// 2. Perform a find operation on just that substring.
auto t = uc.NewTransformer().SetText(contentBlock);
t.Pattern("find");
t.Find();
auto matches = t.Matches();
cout << "Local match StartPosition: " << matches[0].StartPosition() << endl; // Relative to 'contentBlock'
// 3. Apply the offset to remap to the global 'document' coordinate space.
matches.ApplyOffset(startIndex);
cout << "Global match StartPosition: " << matches[0].StartPosition() << endl;
}
Searching within substring: 'data to find'
Local match StartPosition: 8
Global match StartPosition: 22 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; string document = "HEADER::BEGIN[data to find]END::FOOTER"; // 1. Isolate the content block we want to search in. // Assume the block we care about is between BEGIN and END. auto startIndex = document.find("BEGIN[") + 6; auto endIndex = document.find("]END"); auto contentLength = endIndex - startIndex; auto contentBlock = document.substr(startIndex, contentLength); cout << "Searching within substring: '" << contentBlock << "'" << endl; // 2. Perform a find operation on just that substring. auto t = uc.NewTransformer().SetText(contentBlock); t.Pattern("find"); t.Find(); auto matches = t.Matches(); cout << "Local match StartPosition: " << matches[0].StartPosition() << endl; // Relative to 'contentBlock' // 3. Apply the offset to remap to the global 'document' coordinate space. matches.ApplyOffset(startIndex); cout << "Global match StartPosition: " << matches[0].StartPosition() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim Document As String = "HEADER::BEGIN[data to find]END::FOOTER"
'// 1. Isolate the content block we want to search in.
'// Assume the block we care about is between BEGIN and END.
Dim startIndex = document.IndexOf("BEGIN[") + 6
Dim endIndex = document.IndexOf("]END")
Dim contentLength = endIndex - startIndex
Dim contentBlock = document.Substring(startIndex, contentLength)
Console.WriteLine($"Searching within substring: '{contentBlock}'")
'// 2. Perform a find operation on just that substring.
Dim t = uc.NewTransformer().SetText(contentBlock)
t.Pattern("find")
t.Find()
Dim matches = t.Matches
Console.WriteLine($"Local match StartPosition: {matches(0).StartPosition}") '// Relative to 'contentBlock'
'// 3. Apply the offset to remap to the global 'document' coordinate space.
matches.ApplyOffset(startIndex)
Console.WriteLine($"Global match StartPosition: {matches(0).StartPosition}")
End Sub
End Module
Searching within substring: 'data to find'
Local match StartPosition: 8
Global match StartPosition: 22 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim Document As String = "HEADER::BEGIN[data to find]END::FOOTER" '// 1. Isolate the content block we want to search in. '// Assume the block we care about is between BEGIN and END. Dim startIndex = document.IndexOf("BEGIN[") + 6 Dim endIndex = document.IndexOf("]END") Dim contentLength = endIndex - startIndex Dim contentBlock = document.Substring(startIndex, contentLength) Console.WriteLine($"Searching within substring: '{contentBlock}'") '// 2. Perform a find operation on just that substring. Dim t = uc.NewTransformer().SetText(contentBlock) t.Pattern("find") t.Find() Dim matches = t.Matches Console.WriteLine($"Local match StartPosition: {matches(0).StartPosition}") '// Relative to 'contentBlock' '// 3. Apply the offset to remap to the global 'document' coordinate space. matches.ApplyOffset(startIndex) Console.WriteLine($"Global match StartPosition: {matches(0).StartPosition}") End Sub End Module