The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




Thread: regex
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 10-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Refer to the man page section on "Pattern Matching", otherwise known in Shell parlance as "fileglob". The [0-9] is right, but "+" doesn't work here. To match a non-zero digit followed by more digits, you need:

Code:
shopt -s nullglob
echo [1-9][0-9]*

If you can have leading zeros, but you don't want to match one with ONLY 0's, then you can do:

Code:
shopt -s nullglob
echo [1-9][0-9]*  [0-9]*[1-9][0-9]*  [0-9]*[1-9]

This last one matches : (1) any number starting with a non-zero, AND (2) any number with a non-zero in the middle, AND (3) any number ending with a non-zero.

The shopt -s nullglob ensures that if the expression does not match, you get the empty string (instead of the pattern itself).