frankliuao
frankliuao

Reputation: 420

BSD expr difference with GNU expr

Does any one know why

expr "4" : '-\?[0-9]\+$' 

returns 0 on Mac OS X and 1 on Linux?

Fact: Mac uses BSD expr Linux uses GNU

Sorry, I originally typed

expr "4" : '-\?[0-9]+$'

Upvotes: 1

Views: 361

Answers (1)

chepner
chepner

Reputation: 532268

expr takes a basic regular expression, not an extended regular expression. (See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html for the definition of each.)

Basic regular expressions do not support the either the ? and + operators; you need to use bounds instead.

  • ? is implemented with \{0,1\} (0 to 1 occurrences)
  • + is implemented with \{1,\} (1 or more occurrences)

GNU expr appears to allow them as an extension if they are escaped.

The following is a portable call that should work in any POSIX-compliant implementation of expr:

expr "4" : '-\{0,1\}[0-9]\{1,\}$'

Upvotes: 2

Related Questions