Forum Discussion

Dharshana_11419's avatar
Dharshana_11419
Icon for Nimbostratus rankNimbostratus
Nov 20, 2017

findstr - sub string of last occurrence of a character to the end of the string

Hi, Can someone please help me how to find the last occurrence of a character and get a sub string from thereafter to the end of the string using findstr? i.e from XXX-GHDF-12XD45 i need to match 12XD45

 

Many thanks, Dharshana

 

5 Replies

  • set str "XXX-GHDF-12XD45"
    set sub_str [string range $str [string last "-" $str]+1 end]
    

    or

    set str "XXX-GHDF-12XD45"
    set sub_str [lindex [set str_list [split $str "-"]] [llength $str_list]-1]
    
    • Stanislas_Piro2's avatar
      Stanislas_Piro2
      Icon for Cumulonimbus rankCumulonimbus

      I also watched

      getfield
      command but didn't find how to say the last field.

    • Samir_Jha_52506's avatar
      Samir_Jha_52506
      Icon for Noctilucent rankNoctilucent

      You are right. I am just capturing position value with getfield function.

       

  • set listOfStr [split "XXX-GHDF-12XD45" "-"]
    set location [expr {[llength $listOfStr] - 2}
    set output [lindex $listOfStr $location]