Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: string match simplification?





On 2024-09-03 08:58, Mark J. Reed wrote:
First, think about the way the `#` and `%` operators work: they _remove_ matching strings from the parameter value when expanding it. So if there is no matching string, they don't remove anything. That seems completely intuitive; it would be weird for it to remove _everything_ when there's no match.
Ah!  Yes, looked at that way, of course it is intuitive.  I'm thinking 'grab the match' not 'remove what doesn't match' ... and frankly I know better.  I get so rusty. 


If your goal is to extract the extension if there is one, you might be better served by a regex match.

[[ $filename =~ '\.(.*)' ]]

extension=$match[1]


That way, $extension be empty if there's no extension.
Looks good, but that grabs the longest match and I want the shortest.
Gotta get more familiar with regex!



Messages sorted by: Reverse Date, Date, Thread, Author