zsh-workers
 help / color / Atom feed
* Handling of escaped plus sign ("+") in regular expressions
@ 2020-05-20 19:18 Marco Trevisan
  2020-05-20 19:51 ` Eric Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Marco Trevisan @ 2020-05-20 19:18 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1: Type: text/plain, Size: 557 bytes --]

Hi,

I've been using zsh to run some local scripts I was normally running with
bash and while most of them work as expected, there's a case that weirdly
failed to me (using zsh 5.8):

[[ +123 =~ ^+[0-9]+$ ]]
zsh: failed to compile regex: Invalid preceding regular expression

[[ +123 =~ ^\+[0-9]+$ ]]
zsh: failed to compile regex: Invalid preceding regular expression

The same behavior seems to apply both using RE_MATCH_PCRE and not.
As said, both matches correctly using bash.

Using something like

[[ +123 =~ ^[+][0-9]+$ ]]

Works as expected instead.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Handling of escaped plus sign ("+") in regular expressions
  2020-05-20 19:18 Handling of escaped plus sign ("+") in regular expressions Marco Trevisan
@ 2020-05-20 19:51 ` Eric Cook
  2020-05-20 20:00   ` Eric Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Cook @ 2020-05-20 19:51 UTC (permalink / raw)
  To: zsh-workers

On 5/20/20 3:18 PM, Marco Trevisan wrote:
> Hi,
>
> I've been using zsh to run some local scripts I was normally running with
> bash and while most of them work as expected, there's a case that weirdly
> failed to me (using zsh 5.8):
>
> [[ +123 =~ ^+[0-9]+$ ]]
> zsh: failed to compile regex: Invalid preceding regular expression
>
> [[ +123 =~ ^\+[0-9]+$ ]]
> zsh: failed to compile regex: Invalid preceding regular expression
>
> The same behavior seems to apply both using RE_MATCH_PCRE and not.
> As said, both matches correctly using bash.
>
> Using something like
>
> [[ +123 =~ ^[+][0-9]+$ ]]
>
> Works as expected instead.
>

bash's [[ =~ uses regex from the libc it was compiled against last time i checked.
and with libc's for the systems i have available, that won't work. it doesn't spit
out an error message like zsh does, but it correctly does not match the invalid regex.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Handling of escaped plus sign ("+") in regular expressions
  2020-05-20 19:51 ` Eric Cook
@ 2020-05-20 20:00   ` Eric Cook
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Cook @ 2020-05-20 20:00 UTC (permalink / raw)
  To: zsh-workers

On 5/20/20 3:51 PM, Eric Cook wrote:
> [[ +123 =~ ^\+[0-9]+$ ]]
Guessing this is the one that was excepted to return 0
in zsh [[ +123 =~ '^\+[0-9]+$' ]] would be what you wanted.

a safe way of compensate for the quoting differences of the left hand operand
is to put the ere into a variable.
pat='^\+[0-9]+$'; [[ +123 =~ $pat ]]; echo $?

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-20 19:18 Handling of escaped plus sign ("+") in regular expressions Marco Trevisan
2020-05-20 19:51 ` Eric Cook
2020-05-20 20:00   ` Eric Cook

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git