* Regular expression expanding and matching
@ 2012-11-25 15:45 Mark van Dijk
2012-11-25 18:30 ` Vin Shelton
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Mark van Dijk @ 2012-11-25 15:45 UTC (permalink / raw)
To: zsh-users
Hello,
I was trying to match a string with a regular expression as follows:
---
#!/usr/local/bin/zsh
zmodload zsh/pcre
somestring="121125"
todaysday="25"
yesterday="24"
set -xv
if [[ $somestring -pcre-match \d{4}${todaysday} ]]; then
echo "somestring matches today"
elif [[ $somestring -pcre-match \d{4}${yesterday} ]]; then
echo "somestring matches yesterday"
fi
set +xv
---
Relevant output:
---
+./zshtest:9> [[ $somestring -pcre-match \d{4}${todaysday} ]]
+./zshtest:11> [[ $somestring -pcre-match \d{4}${yesterday} ]]
---
Apparently there is no expansion of ${todaysday} and ${yesterday}. This
is not really surprising because in regular expressions many characters
have a different meaning.
So what's the proper way to achieve this type of expression matching?
Thank you,
Mark
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Regular expression expanding and matching
2012-11-25 15:45 Regular expression expanding and matching Mark van Dijk
@ 2012-11-25 18:30 ` Vin Shelton
2012-11-25 19:09 ` Mark van Dijk
2012-11-25 18:57 ` Peter Stephenson
2012-11-25 19:30 ` Bart Schaefer
2 siblings, 1 reply; 6+ messages in thread
From: Vin Shelton @ 2012-11-25 18:30 UTC (permalink / raw)
To: Mark van Dijk; +Cc: zsh-users
On Sun, Nov 25, 2012 at 10:45 AM, Mark van Dijk
<lists+zsh@internecto.net> wrote:
> Apparently there is no expansion of ${todaysday} and ${yesterday}.
> This is not really surprising because in regular expressions many
> characters have a different meaning.
>So what's the proper way to achieve this type of expression matching?
Enclosing the expressions in double quotes seems to work for me:
if [[ $somestring -pcre-match "\d{4}${todaysday}" ]]; then
echo "somestring matches today"
elif [[ $somestring -pcre-match "\d{4}${yesterday}" ]]; then
echo "somestring matches yesterday"
fi
+./tt:8> [[ $somestring -pcre-match "\d{4}${todaysday}" ]]
+./tt:9> echo 'somestring matches today'
somestring matches today
Regards,
Vin
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Regular expression expanding and matching
2012-11-25 15:45 Regular expression expanding and matching Mark van Dijk
2012-11-25 18:30 ` Vin Shelton
@ 2012-11-25 18:57 ` Peter Stephenson
2012-11-25 19:18 ` Vin Shelton
2012-11-25 19:30 ` Bart Schaefer
2 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2012-11-25 18:57 UTC (permalink / raw)
To: zsh-users
On Sun, 25 Nov 2012 16:45:49 +0100
Mark van Dijk <lists+zsh@internecto.net> wrote:
> I was trying to match a string with a regular expression as follows:
>
> ---
> #!/usr/local/bin/zsh
> zmodload zsh/pcre
> somestring="121125"
> todaysday="25"
> yesterday="24"
>
> set -xv
> if [[ $somestring -pcre-match \d{4}${todaysday} ]]; then
> echo "somestring matches today"
> elif [[ $somestring -pcre-match \d{4}${yesterday} ]]; then
> echo "somestring matches yesterday"
> fi
> set +xv
>
> Apparently there is no expansion of ${todaysday} and ${yesterday}. This
> is not really surprising because in regular expressions many characters
> have a different meaning.
I think the problem is different from what you think it is. Try (note
doubled backslashes):
if [[ $somestring -pcre-match \\d{4}${todaysday} ]]; then
echo "somestring matches today"
elif [[ $somestring -pcre-match \\d{4}${yesterday} ]]; then
echo "somestring matches yesterday"
fi
It's not that the $... isn't expanded, it's that the \d is also handled
like a normal command line argument. Vin's change works because in
double quotes \d remains \d because it doesn't have a special meaning.
To put it more broadly, arguments in [[ ... ]] get expanded in all the ways that make sense for generating a single word. So file name generation (globbing)
doesn't happen, and array substitution produces a single word (as if double
quotes surrounded the array), but otherwise it works like a normal command line argument. So the normal quoting rule for backslashes apply.
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Regular expression expanding and matching
2012-11-25 18:30 ` Vin Shelton
@ 2012-11-25 19:09 ` Mark van Dijk
0 siblings, 0 replies; 6+ messages in thread
From: Mark van Dijk @ 2012-11-25 19:09 UTC (permalink / raw)
To: Vin Shelton; +Cc: zsh-users
On 25-11-12 19:30, Vin Shelton wrote:
> On Sun, Nov 25, 2012 at 10:45 AM, Mark van Dijk
> <lists+zsh@internecto.net> wrote:
>> Apparently there is no expansion of ${todaysday} and ${yesterday}.
>> This is not really surprising because in regular expressions many
>> characters have a different meaning.
>
>> So what's the proper way to achieve this type of expression matching?
>
> Enclosing the expressions in double quotes seems to work for me:
Argh. Painfully easy. Thanks Vin!
Mark
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Regular expression expanding and matching
2012-11-25 18:57 ` Peter Stephenson
@ 2012-11-25 19:18 ` Vin Shelton
0 siblings, 0 replies; 6+ messages in thread
From: Vin Shelton @ 2012-11-25 19:18 UTC (permalink / raw)
To: Peter Stephenson; +Cc: zsh-users
Peter -
On Sun, Nov 25, 2012 at 1:57 PM, Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
> To put it more broadly, arguments in [[ ... ]] get expanded in all
> the ways that make sense for generating a single word. So file name
> generation (globbing) doesn't happen, and array substitution
> produces a single word (as if double quotes surrounded the array),
> but otherwise it works like a normal command line argument. So the
> normal quoting rule for backslashes apply.
That is very well put - thanks for the neat summary.
- Vin
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Regular expression expanding and matching
2012-11-25 15:45 Regular expression expanding and matching Mark van Dijk
2012-11-25 18:30 ` Vin Shelton
2012-11-25 18:57 ` Peter Stephenson
@ 2012-11-25 19:30 ` Bart Schaefer
2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2012-11-25 19:30 UTC (permalink / raw)
To: Mark van Dijk, zsh-users@zsh.org
On Nov 25, 4:45pm, Mark van Dijk wrote:
}
} if [[ $somestring -pcre-match \d{4}${todaysday} ]]; then
} echo "somestring matches today"
} elif [[ $somestring -pcre-match \d{4}${yesterday} ]]; then
} echo "somestring matches yesterday"
} fi
}
} Apparently there is no expansion of ${todaysday} and ${yesterday}.
No, that's not what's happening here. What's happening is that the
backslash is being removed by the shell command line parser before
the regular expression is passed to the PCRE parser. Try
if [[ $somestring -pcre-match \\d{4}${todaysday} ]]; then
echo "somestring matches today"
elif [[ $somestring -pcre-match \\d{4}${yesterday} ]]; then
echo "somestring matches yesterday"
fi
} +./zshtest:9> [[ $somestring -pcre-match \d{4}${todaysday} ]]
} +./zshtest:11> [[ $somestring -pcre-match \d{4}${yesterday} ]]
Hmm, there's something slighly off about that XTRACE output. Change
the condition to -ef and you can see the real expansion:
+./zshtest:9> [[ 121125 -ef 'd{4}25' ]]
+./zshtest:11> [[ 121125 -ef 'd{4}24' ]]
I'm not sure where the trace output is generated in the case of an infix
condition operator that comes from a module, but I think there is a bug.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-11-25 19:30 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-25 15:45 Regular expression expanding and matching Mark van Dijk
2012-11-25 18:30 ` Vin Shelton
2012-11-25 19:09 ` Mark van Dijk
2012-11-25 18:57 ` Peter Stephenson
2012-11-25 19:18 ` Vin Shelton
2012-11-25 19:30 ` Bart Schaefer
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).