Forum Discussion

Carl_Weiss_1340's avatar
Carl_Weiss_1340
Icon for Nimbostratus rankNimbostratus
Jun 28, 2016
Solved

String Scan question ... trying to do something really simple

Its just not working...

 

Given a string - "/part1/part2/part3/part4" I want the result to be: p1 = part1 p2 = part2 p3 = /part3/part4

 

which led me to write the following:

 

when HTTP_REQUEST { set foo "/part1/part2/part3/part4"

 

if { [ scan $foo {/%s/%s/%s} part1 part2 part3 ] == 3 } { log local0. "testing part1 = $part1; part2= $part2; part3 = $part3" } else { log local0. "did not scan properly" } log local0. "exit irule" }

 

I tried scan $foo {/%[^/]/%[^/]/%s} - which did not work either. So its something in the expression that I have wrong...

 

I keep getting "did not scan properly" in the logs

 

Any help or pointers appreciated.

 

Thanks Carl

 

  • String matchers are "greedy", so %s will match everything up to the next whitespace. That's why /%s/%s/%s won't work; everything ends up in $part1.

    The second construct should work. Indeed, on 11.5.4, I do the following:

    when CLIENT_ACCEPTED {
        set path "/foo/bar/baz/bing"
        
        set x [scan $path {/%[^/]/%[^/]/%s} part1 part2 part3]
        if { $x == 3 } {
            log local0. "part1 = $part1; part2 = $part2; part3 = $part3"
        }
        else {
            log local0. "scan returned ($x) elements"
        }
    }
    

    And it works as I expect. In the logs, I get:

    Jun 27 19:00:01 b201 info tmm[13446]: Rule /Common/test_scan : part1 = foo; part2 = bar; part3 = baz/bing
    

4 Replies

  • String matchers are "greedy", so %s will match everything up to the next whitespace. That's why /%s/%s/%s won't work; everything ends up in $part1.

    The second construct should work. Indeed, on 11.5.4, I do the following:

    when CLIENT_ACCEPTED {
        set path "/foo/bar/baz/bing"
        
        set x [scan $path {/%[^/]/%[^/]/%s} part1 part2 part3]
        if { $x == 3 } {
            log local0. "part1 = $part1; part2 = $part2; part3 = $part3"
        }
        else {
            log local0. "scan returned ($x) elements"
        }
    }
    

    And it works as I expect. In the logs, I get:

    Jun 27 19:00:01 b201 info tmm[13446]: Rule /Common/test_scan : part1 = foo; part2 = bar; part3 = baz/bing
    
    • Carl_Weiss_1340's avatar
      Carl_Weiss_1340
      Icon for Nimbostratus rankNimbostratus

      sorry for the tardiness - I tried the alternative again and viola! it worked (not sure what happened when I tested it before).

       

  • Vernon_97235's avatar
    Vernon_97235
    Historic F5 Account

    String matchers are "greedy", so %s will match everything up to the next whitespace. That's why /%s/%s/%s won't work; everything ends up in $part1.

    The second construct should work. Indeed, on 11.5.4, I do the following:

    when CLIENT_ACCEPTED {
        set path "/foo/bar/baz/bing"
        
        set x [scan $path {/%[^/]/%[^/]/%s} part1 part2 part3]
        if { $x == 3 } {
            log local0. "part1 = $part1; part2 = $part2; part3 = $part3"
        }
        else {
            log local0. "scan returned ($x) elements"
        }
    }
    

    And it works as I expect. In the logs, I get:

    Jun 27 19:00:01 b201 info tmm[13446]: Rule /Common/test_scan : part1 = foo; part2 = bar; part3 = baz/bing
    
    • Carl_Weiss_1340's avatar
      Carl_Weiss_1340
      Icon for Nimbostratus rankNimbostratus

      sorry for the tardiness - I tried the alternative again and viola! it worked (not sure what happened when I tested it before).