From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 1461b125 for ; Fri, 16 Aug 2019 01:13:03 +0000 (UTC) Received: (qmail 9212 invoked by alias); 15 Aug 2019 18:48:05 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 24144 Received: (qmail 29414 invoked by uid 1010); 15 Aug 2019 18:48:05 -0000 X-Qmail-Scanner-Diagnostics: from mail-ua1-f54.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.101.2/25538. spamassassin: 3.4.2. Clear:RC:0(209.85.222.54):SA:0(-2.0/5.0):. Processed in 2.770493 secs); 15 Aug 2019 18:48:05 -0000 X-Envelope-From: sgniazdowski@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.222.54 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=nNE9XdGGEAbt0LQnNKWUe79mHNCMZtnaLb6F0kwUPj8=; b=jNtNFWtKo8mDo3K4ZwzssXYT19UFRsrw0ZO7EaL5uEici4ABzjI+1AFpuR3PPkKGwz 5Cv2AF4afq6UwLZTDADahdaqnf5jqQxt7rxTI6FMHbM+cRZxeAFYpcsD/SAeqZKBqOjk 6nrG9+uorC9MC1wkPT2fby2wGcQ1VmUCQfoz4xJjWQrrBkdJS10H0CJulO06RklDfveg 3ns1Q7DHI3UFvLZvRMqTHmaSxifHCpxXTHam+FJRGkNCl+ZnEfG8UrjlMhngQTRTIaIx M6RYS+EQwqz3IIkUcM+AkEceOSjdA2u5M8ldNb5v9dm+HoXABnulHoaYrl7XHsh6oNF7 HKAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=nNE9XdGGEAbt0LQnNKWUe79mHNCMZtnaLb6F0kwUPj8=; b=DTAjhOyJKVSBCLZBEyL1JjU2p4YYv8376fdEaAnqI20ecEudQG8VT00qpVIP0wQBPx 1/3pZPuFSsrSdJUeXEP58Pn8LDFXhnoAVth+NzqtR4HWc4CW/WpKWBA+1bP3rdI4uuAN /FmTFxJPxfRFTKgD2sb85GRXqA6Bkn+AeJ5uHyfCkGoLlIB3l6uaTuNhqoyLsudB/jGH kBz3etsk5420q5CiZdB/PJyHWXxtEXZDCCAsTEQl73t0qCygaCAnlb3saQE1/ZPSMxm+ 9tVfIHNPfEvSTReiiQWtpPDmkX8cbySGYJFxitpFor5IdPZ/cUTEOEkMQZgHB98dJqeq xyeg== X-Gm-Message-State: APjAAAU6IASFB/G81cGgzzi4ZueAYl5I+Ng7izzBF2y8Uew80TDBA26z pJDtiUtAIiN4ZrCiWvxnaaMq6jpNKB9uYdZOzm8A+zgdFpg= X-Google-Smtp-Source: APXvYqwtP+jwqybtUJUp9z1PpWIdb8UJZNp3TuLuNGC1VKwcj12Nf8Mwfr5sfQiePsYSXSebeYKzD3awc34m7Pg3cls= X-Received: by 2002:ab0:2511:: with SMTP id j17mr2364610uan.28.1565894849391; Thu, 15 Aug 2019 11:47:29 -0700 (PDT) MIME-Version: 1.0 From: Sebastian Gniazdowski Date: Thu, 15 Aug 2019 20:47:18 +0200 Message-ID: Subject: Useful functions for non-greedy matching To: Zsh Users Content-Type: text/plain; charset="UTF-8" Hello, Zsh supports non greedy matching only in substitutions like //, via the (S) flag. This however turns out to be not that big problem thanks to the following functions: .smatch() { local str="$1" pat="$2" retval=1 match=() : ${(S)str/(#b)(${~pat})/${retval::=0}} REPLY="${match[1]}" return $retval } .smatches() { local pat="${@[${#}]}" retval=1 local -a input input=( "${@[1,${#}-1]}" ) reply=() match=() : "${(S)input[@]//(#b)(${~pat})/${reply[${#reply}+1]::=${match[1]}}${retval::=0}}" REPLY="${match[1]}" return $retval } Example usage: arr=( a1xx ayy a2xx ) if .smatches ${arr[@]} "a*x"; then print -rl $reply fi Outout: a1x a2x Gist for possible updates: https://gist.github.com/psprint/a69177329d6d15bffdd320011aa208da -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin Blog: http://zdharma.org