String Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > String commands > !STR.- String Command > Instring - Find Commands >

String Operations

STR.RNInstr

Previous Top Next


MiniRobotLanguage (MRL)

 

STR.RNInstr

Reverse Nth String Search (Right-to-Left)

 

Purpose

Finds the Nth occurrence of a substring (P2) in a source string (P1), scanning from right to left (i.e., starting near the end and moving backward).

You can:Limit the search to a prefix of the string (P4)Start scanning from a specific distance from the end (P7)Ignore case (P5)Allow overlapping matches (P6)

The result is the 1-based position from the start of the match, or 0 if not found. The result is always pushed to the Top of Stack (TOS) and optionally stored in a variable (P8).

 

Syntax

 

STR.RNInstr|P1|P2|P3[|P4][|P5][|P6][|P7][|P8]

 

Parameter Details

 

P1 – Source string (variable or literal).

P2 – Substring to find (variable or literal).

P3 – Occurrence number (1 = last match, 2 = second-to-last, etc.). Values ≤ 0 are treated as 1.

P4 – (Optional) Left-bound limit. Only positions 1 to P4 are searched. Default = full string length.

P5 – (Optional) Case-insensitive flag. 0 = case-sensitive (default), non-zero = case-insensitive (ASCII letters only).

P6 – (Optional) Overlap flag. 0 = non-overlapping (default), non-zero = allow overlapping matches.

P7 – (Optional) Start position counted from the right. 1 = last character, 2 = second-to-last, etc. Default = 1 (start from end).

P8 – (Optional) Variable name to store the result. Result is always pushed to TOS.

 

How It Works

The command scans backward from the effective start position:

effectiveStart = MIN(P4, LEN(P1) - P7 + 1)

It then finds matches moving leftward, counting each one until it reaches the Nth.

 

 

Test Scripts (Copy & Run)

'==================================================

' STR.RNInstr - Self-Running Test (5-char vars)

'==================================================

 

'--- Test 1: Last "Hello" in "HelloWorldHello"

$$TXT=HelloWorldHello

STR.RNInstr|$$TXT|Hello|1|0|0|0|1|$$LAS

' Expected: 11

PRT.$$LAS

 

'--- Test 2: Second-to-last "Hello"

STR.RNInstr|$$TXT|Hello|2|0|0|0|1|$$SEC

' Expected: 1

PRT.$$SEC

 

'--- Test 3: Case-insensitive + overlapping — 3rd "A" from end in "AbAbAbA"

$$STR=AbAbAbA

STR.RNInstr|$$STR|A|3|0|1|1|1|$$POS

' Expected: 3

PRT.$$POS

 

'--- Test 4: Start from 3rd char from end in "abcdefg", find "c"

$$SRC=abcdefg

STR.RNInstr|$$SRC|c|1|0|0|0|3|$$ST3

' Expected: 3

PRT.$$ST3

 

'--- Test 5: No match

STR.RNInstr|Hello|xyz|1|0|0|0|1|$$NOM

' Expected: 0

PRT.$$NOM

 

'--- Test 6a: Overlap in "aaaa" ? 2nd from right = 3

$$AAA=aaaa

STR.RNInstr|$$AAA|a|2|0|0|1|1|$$OVL

PRT.$$OVL

' ? 3

 

'--- Test 6b: No overlap in "aaaa" ? 2nd from right = 2

STR.RNInstr|$$AAA|a|2|0|0|0|1|$$NOL

PRT.$$NOL  

' ? 2

MBX.!

ENR.

 

Important Notes

Always searches right-to-left — there is no forward mode.

P7 = 1 means “start at the very end”. P7 = 3 means “start at the 3rd character from the end”.

If P4 is smaller than the effective start position, the scan begins at P4.

Result is always 1-based from the start of the string — never negative.

Result is always pushed to TOS, even if P8 is provided.

Case-insensitive mode only affects A–Z / a–z. Other characters are compared exactly.

 

Limitations

No wildcard, regex, or Unicode case folding.

Maximum string length limited by system memory (practical limit: ~100 MB).

 

See also

STR.NInstr – Forward (left-to-right) Nth search

STR.CiInstr – Case-insensitive forward search

STR.FindAny – Wildcard/pattern search

The Stack (TOS)