1

I want to check if the pattern ends with "/" or "\" and before it can be any string.

I tried this

*\/  

* for any string, \ for removing meaning of "/" and / for "/"

2

Try this:

[/\\]$

That is, a forward slash or a backward slash, followed by end-of-string.

There are many "flavours" of regular expressions: what tool are you using?

  • what do you mean by a tool?.. It looks like it doesn't work but that's probably because I am trying to use it in "if condition" :( – applenic Mar 28 '15 at 12:29
  • bash? [[ $string =~ [/\\]$ ]] – glenn jackman Mar 28 '15 at 12:59
  • The problem is it needs to work in any shell. But with bash this works. Thanks again. – applenic Mar 28 '15 at 13:21
  • If it needs to work in any shell, then you can't rely on the shell to test it: you have to call an external program: expr "$string" : '.*[/\\]$' may be the most portable test. – glenn jackman Mar 28 '15 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.