Regex to find text between second and third slashes

后端 未结 6 1967
夕颜
夕颜 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:21

    The regex would be:

    /[^/]*/([^/]*)/
    

    so in Perl, the regex capture statement would be something like:

    ($database) = $text =~ m!/[^/]*/([^/]*)/!;
    

    Normally the / character is used to delimit regexes but since they're used as part of the match, another character can be used. Alternatively, the / character can be escaped:

    ($database) = $text =~ /\/[^\/]*\/([^\/]*)\//;
    

提交回复
热议问题