1

I have a small problem I really can't understand :

bash -c 'if [[ "hello" =~ ^[a-zA-Z0-9]\{1,\}\\.$ ]] ; then echo "OK" ; else echo "KO" ; fi

I think this should give me KO and it gives me OK... I would like to match things with at least 1 character and ending with a dot...

I finally noticed that it works with bash version 4.1.5 and not with version 3.2.25

How should I proceed with this version ?

EDIT :

I found a workaround that works, but I don't know why I had to put the escaped dot between brackets:

bash -c 'if [[ "hello" =~ ^[a-zA-Z0-9]{1,}[\.]$ ]] ; then echo "OK" ; else echo "KO" ; fi'
ssapkota
  • 3,262
  • 19
  • 30
Lapin-Blanc
  • 1,945
  • 1
  • 17
  • 26

2 Answers2

3

You did not escape the dot, so it is used as a wildcard and matches any character. Replace the . with \. Also, instead of {1,}, use +, because they are equivalent.

Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
  • Thanks for the answer, actually I did escape the dot, but it doesn't show in here (corrected now with double backslash...) Also, bash regexp doesn't accept + as a modifier... – Lapin-Blanc Jun 26 '11 at 18:29
  • @Lapin-Blanc the way it is now it works: http://i54.tinypic.com/2l9s561.png (used copy-paste from the question) – Gabi Purcaru Jun 26 '11 at 18:32
  • I guess you tried with a more recent version of bash, i'm on Centos 5.6 and ... http://i53.tinypic.com/21jd2ew.png :-) – Lapin-Blanc Jun 26 '11 at 18:41
1

. is special in regular expressions ("match any characters"). Escape it as \.

geekosaur
  • 59,309
  • 11
  • 123
  • 114