Test if last character in variable is a '/' or '\'

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Test if last character in variable is a '/' or '\'

Jeff Lanzarotta
Hello,

I have a script were I need to check if the last character in a
variable length variable is a '/' or a '\' and if it is then do
something... I have no idea how to check this? Any ideas?

Thanks,

-Jeff
Reply | Threaded
Open this post in threaded view
|

Re: Test if last character in variable is a '/' or '\'

Jürgen Krämer

Hi,

Jeff Lanzarotta schrieb:
>
> I have a script were I need to check if the last character in a
> variable length variable is a '/' or a '\' and if it is then do
> something... I have no idea how to check this? Any ideas?

  if variable =~ '[/\\]$'
    echo 'found (back)slash'
  endif

Regards,
J?rgen

--
J?rgen Kr?mer                              Softwareentwicklung
HABEL GmbH & Co. KG                        mailto:[hidden email]
Hinteres ?schle 2                          Tel: +49 / 74 61 / 93 53 - 15
78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99
Reply | Threaded
Open this post in threaded view
|

Re: Test if last character in variable is a '/' or '\'

Tim Chase-2
In reply to this post by Jeff Lanzarotta
> I have a script were I need to check if the last character in a
> variable length variable is a '/' or a '\' and if it is then do
> something... I have no idea how to check this? Any ideas?

two couple ideas (assuming "abc" is your variable in question):

   if (abc[strlen(abc)-1])=~'[/\\]' ...

or

   if (match(abc, '[/\\]$')!=-1) ...

Just a few ideas,

-tim



Reply | Threaded
Open this post in threaded view
|

Re: Test if last character in variable is a '/' or '\'

Jeff Lanzarotta
In reply to this post by Jürgen Krämer
Wow! that was so easy... Thanks.

--- J?rgen Kr?mer <[hidden email]> wrote:

>
> Hi,
>
> Jeff Lanzarotta schrieb:
> >
> > I have a script were I need to check if the last character in a
> > variable length variable is a '/' or a '\' and if it is then do
> > something... I have no idea how to check this? Any ideas?
>
>   if variable =~ '[/\\]$'
>     echo 'found (back)slash'
>   endif
>
> Regards,
> J?rgen
>
> --
> J?rgen Kr?mer                              Softwareentwicklung
> HABEL GmbH & Co. KG                        mailto:[hidden email]
> Hinteres ?schle 2                          Tel: +49 / 74 61 / 93 53 -
> 15
> 78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 -
> 99
>