Automation

 View Only
last person joined: 8 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Looking for a function that returns the position of a string within a string

    Posted 11-01-2024 13:39

    Hello all. 

    I am looking for a function that would return the position of a string within a string to be used in a slax script. I've been searching in the documentation but can't find anything close to what I want. 

    There is the "contains" function which will tell me if one string exists within another string but can't find one that would return the position. I can write my own, but why reinvent the wheel if one exists. 

    Thanks. 



    ------------------------------
    YVON LEDUC
    ------------------------------


  • 2.  RE: Looking for a function that returns the position of a string within a string

     
    Posted 11-02-2024 19:53
    Edited by spuluka 11-03-2024 14:22

    The following will perform the task:

    version 1.2;
    
    main <op-script-results> {
        <output> {
            var $string = "The quick brown fox jumps over the lazy dog";
            var $search = "over";
    
            if (contains($string, $search)) {
                var $pos = string-length(substring-before($string, $search))+1;
                expr "\nString:" _ $string _ "\n";
                expr "Search:" _ $search _ "\n";
                expr "Position:" _ $pos _ "\n";
                expr "Test:" _ substring($string, $pos, string-length($search)) _ "\n";
            }
            else {
                expr "\nString not found!\n";
            }
        }
    }
    
    Results:
    juise string_pos.slax
    <?xml version="1.0"?>
    <op-script-results><output>
    String:The quick brown fox jumps over the lazy dog
    Search:over
    Position:27
    Test:over
    </output></op-script-results>




  • 3.  RE: Looking for a function that returns the position of a string within a string

    Posted 11-03-2024 12:23

    Thanks ASHARP for your solution even thought somehow, it does not appears in the post. 

     I guess I am slowing down with old age but so used to have an actual function at my disposal. I was able to reach my goal using your idea. 

    Thanks. 

    His solution was mainly to use a combination of substring-before and string-length to get a similar result. 

    pos("ABCDEF", "DEF") = string-length(substring-before("ABCDEF", "DEF")+1)



    ------------------------------
    YVON LEDUC
    ------------------------------



  • 4.  RE: Looking for a function that returns the position of a string within a string

     
    Posted 11-03-2024 14:23

    Sure, no problem.  I think that my reply takes longer to register because it probably triggered some additional checks because of code, or spam filter or something, not really sure why that happens, but when I saw my first reply appear, I made a change to it to tidy the code and add a if/else clause, and I guess that that again triggered the checks....  So I suspect that the updated post will appear either later today or tomorrow...  But you got the idea from reply, that using string-length(substring-before())+1 will provide you with the positional index for the substring you are looking for.  The if/else clause I added was just to use a contains() to see if the substring is present or not, else you'll always get a value of 1 based on the formula.  So better to check if the string is present first, and then calculate the position etc.

    Regards.