Обсуждение: How best to work around the issue - regex string cannot contain brackets

Поиск
Список
Период
Сортировка

How best to work around the issue - regex string cannot contain brackets

От
Shaozhong SHI
Дата:
One would consider the following would work, but it did not because the brackets.
select regexp_matches('Department for Transport (Parking)', 'Department for Transport (Parking)', 'g')

Can anyone enlighten me?

Regards,

David

Re: How best to work around the issue - regex string cannot contain brackets

От
Christophe Pettus
Дата:

> On Feb 3, 2022, at 08:53, Shaozhong SHI <shishaozhong@gmail.com> wrote:
>
> One would consider the following would work, but it did not because the brackets.
> select regexp_matches('Department for Transport (Parking)', 'Department for Transport (Parking)', 'g')
>
> Can anyone enlighten me?

You escape the ()s with a backslash:

xof=# select regexp_matches('Department for Transport (Parking)', 'Department for Transport \(Parking\)', 'g');
             regexp_matches
----------------------------------------
 {"Department for Transport (Parking)"}
(1 row)




Re: How best to work around the issue - regex string cannot contain brackets

От
"David G. Johnston"
Дата:
On Thu, Feb 3, 2022 at 9:58 AM Christophe Pettus <xof@thebuild.com> wrote:


> On Feb 3, 2022, at 08:53, Shaozhong SHI <shishaozhong@gmail.com> wrote:
>
> One would consider the following would work, but it did not because the brackets.
> select regexp_matches('Department for Transport (Parking)', 'Department for Transport (Parking)', 'g')
>
> Can anyone enlighten me?

Have you tried reading a book or some tutorials on RegExes?  I'll admit our documentation is probably not the best resource out there to actually learn the language.


You escape the ()s with a backslash:


David J.

Re: How best to work around the issue - regex string cannot contain brackets

От
hubert depesz lubaczewski
Дата:
On Thu, Feb 03, 2022 at 04:53:18PM +0000, Shaozhong SHI wrote:
> One would consider the following would work, but it did not because the
> brackets.
> select regexp_matches('Department for Transport (Parking)', 'Department for
> Transport (Parking)', 'g')
> Can anyone enlighten me?

Perhaps you don't want regexp matching, but simple equality or
substring match?

depesz



Re: How best to work around the issue - regex string cannot contain brackets

От
Shaozhong SHI
Дата:
It appears that the following regex work differently.

Why \d and [\d] are different?

[A-PR-UWYZ]\d{1,2} and [A-PR-UWYZ][\d]{1,2}

Regards,

David

On Thu, 3 Feb 2022 at 17:04, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Thu, Feb 3, 2022 at 9:58 AM Christophe Pettus <xof@thebuild.com> wrote:


> On Feb 3, 2022, at 08:53, Shaozhong SHI <shishaozhong@gmail.com> wrote:
>
> One would consider the following would work, but it did not because the brackets.
> select regexp_matches('Department for Transport (Parking)', 'Department for Transport (Parking)', 'g')
>
> Can anyone enlighten me?

Have you tried reading a book or some tutorials on RegExes?  I'll admit our documentation is probably not the best resource out there to actually learn the language.


You escape the ()s with a backslash:


David J.

Re: How best to work around the issue - regex string cannot contain brackets

От
Steve Midgley
Дата:


On Fri, Feb 4, 2022 at 6:01 AM Shaozhong SHI <shishaozhong@gmail.com> wrote:
It appears that the following regex work differently.

Why \d and [\d] are different?

[A-PR-UWYZ]\d{1,2} and [A-PR-UWYZ][\d]{1,2}


This is getting into regex stuff, where maybe stackoverflow is a better resource? But when you put characters into brackets, you are telling regex to search for each character represented in the bracket. So [\d] is looking for any single character that is either a \ or a d character. Outside of brackets, regex evaluates \d as any digit. For US English charset [0-9] is equivalent to \d I believe.

Re: How best to work around the issue - regex string cannot contain brackets

От
"David G. Johnston"
Дата:
On Fri, Feb 4, 2022 at 10:14 AM Steve Midgley <science@misuse.org> wrote:


On Fri, Feb 4, 2022 at 6:01 AM Shaozhong SHI <shishaozhong@gmail.com> wrote:
It appears that the following regex work differently.

Why \d and [\d] are different?

[A-PR-UWYZ]\d{1,2} and [A-PR-UWYZ][\d]{1,2}

Show your work!


But when you put characters into brackets, you are telling regex to search for each character represented in the bracket. So [\d] is looking for any single character that is either a \ or a d character. Outside of brackets, regex evaluates \d as any digit.

This is simply wrong.  Test it.

David J.

Re: How best to work around the issue - regex string cannot contain brackets

От
Steve Midgley
Дата:


On Fri, Feb 4, 2022 at 9:24 AM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Fri, Feb 4, 2022 at 10:14 AM Steve Midgley <science@misuse.org> wrote:


On Fri, Feb 4, 2022 at 6:01 AM Shaozhong SHI <shishaozhong@gmail.com> wrote:
It appears that the following regex work differently.

Why \d and [\d] are different?

[A-PR-UWYZ]\d{1,2} and [A-PR-UWYZ][\d]{1,2}

Show your work!


But when you put characters into brackets, you are telling regex to search for each character represented in the bracket. So [\d] is looking for any single character that is either a \ or a d character. Outside of brackets, regex evaluates \d as any digit.


Apologies - I was relying on stackoverflow and my prior experiences with regex engines that don't honor shorthand characters inside brackets. I should have tested postgres latest before responding. Thanks for the correction. (https://stackoverflow.com/questions/46020936/perl-like-shorthand-character-class-not-working-inside-bracket-expression)