Regex to find text between second and third slashes

后端 未结 6 1975
夕颜
夕颜 2020-11-29 07:48

I would like to capture the text that occurs after the second slash and before the third slash in a string. Example:

/ipaddress/databasename/

I need to capt

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 08:07

    I know you specifically asked for regex, but you don't really need regex for this. You simply need to split the string by delimiters (in this case a backslash), then choose the part you need (in this case, the 3rd field - the first field is empty).

    • cut example:

      cut -d '/' -f 3 <<< "$string"
      
    • awk example:

      awk -F '/' {print $3} <<< "$string"
      
    • perl expression, using split function:

      (split '/', $string)[2]
      

    etc.

提交回复
热议问题