0

I try to use REGEX in bash in order to check if the parameter seems to be a valid path following this format:

/first/second

In java I use with success the following REGEX :

((\\/)[a-zA-Z0-9\\s_@\\-!#$%&+=]+)+

But in bash I have some issue that I can't explain. In order to simplify my comprehension I try to use a simpler REGEX like that:

CHECKPATH="!/first/second"
REGEX_PATH="((\/)[a-zA-Z0-9]+)+"
if [[ ! ${CHECKPATH} =~ $REGEX_PATH ]]; then
    echo "error"
else
    echo "OK"
fi

I insert an "!" in the path just to check. So I should have "error" but not. With or without the "!" it changes nothing, what's the problem?

Update: (moved author's subquestion here from a comment)

One more thing, if I use ^((\/)[a-zA-Z0-9\s_@#&%!$+=\-]+)+$ it's ok, but it's not with ^((\/)[a-zA-Z0-9\s_@\-#&%!$+=]+)+$. I just changed the position of "\-" inside the regex, why?

2
  • 1
    you might need to add ^ and $ for begins with and ends with. try testing it here: regexpal.com Commented Nov 2, 2016 at 16:17
  • Works for me in bash 3.2 and and 4.3, with or with the ! in the value of CHECKPATH. Commented Nov 2, 2016 at 16:20

1 Answer 1

2

Some regex libraries (possible the Java one you use) have methods that specifically test the regex against the whole input string.

But in bash (and generally in regex) you use:

  • ^ - match start of the input, and
  • $ - match end of the input

So instead try,

REGEX_PATH="^((\/)[a-zA-Z0-9]+)+$"

Update:

You have to put the dash on the start/end.

http://man7.org/linux/man-pages/man7/regex.7.html :

To include a literal '-', make it the first or last character, or the second endpoint of a range. To use a literal '-' as the first endpoint of a range, enclose it in "[." and ".]" to make it a collating element (see below).

AND (same link) [emphasis mine]

With the exception of these and some combinations using '[' (see next paragraphs), all other special characters, including '\', lose their special significance within a bracket expression.

Sign up to request clarification or add additional context in comments.

2 Comments

One more thing, if I use ^((\/)[a-zA-Z0-9\s_@#&%!$+=\-]+)+$ it's ok, but it's not with ^((\/)[a-zA-Z0-9\s_@\-#&%!$+=]+)+$. I just change the position of "\-" inside the regex, why ?
@gduh I've moved this subquestion to the original question and added update to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.