Quote:
Originally Posted by
mira
Hi Franklin,
I am curious to know what does this '##*' mean in this regular expression?
Thanks!
This isn't a regex, it's a shell (kshell or bash) variable substitution syntax. The
##pattern and #pattern match the pattern at the beginning of the value contained by the variable and
deletes it before the value is used. (The contents of the variable are unchanged)
The difference between the two is that
## matches the longest pattern and
# matches the shortest. For example, if you had a filename
/home/scooter/lib/cal.rb in variable fname and want the basename, coding
${fname##*/} matches all charcters (*) from the front of the string up to the right most slant. The result would be
cal.rb
If you had coded [${fname#*/} then all characters up to the first slant would be removed and the result would have been
home/scooter/lib/cal.rb.
You will also note that the "pattern" isn't a regular expression pattern, but a file globbing pattern and thus its
##*/ rather than
##.*/. This can be confusing.
The advantage to using these, as opposed to
$(basename $fname), is that it is much more efficient to let the shell do the string manipulation than to invoke a separate process just to chop a string up.
Have a look at the Kshell man page as there are a lot of variable substitution tricks that can be used.
man/man1/ksh.html man page