agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedHow best to work around the issue - regex string cannot contain brackets
8+ messages / 5 participants
[nested] [flat]
* How best to work around the issue - regex string cannot contain brackets
@ 2022-02-03 16:53 Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 2 replies; 8+ messages in thread
From: Shaozhong SHI @ 2022-02-03 16:53 UTC (permalink / raw)
To: pgsql-sql <pgsql-sql@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-03 16:58 Christophe Pettus <xof@thebuild.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 1 reply; 8+ messages in thread
From: Christophe Pettus @ 2022-02-03 16:58 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>
> 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)
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-03 17:04 David G. Johnston <david.g.johnston@gmail.com>
parent: Christophe Pettus <xof@thebuild.com>
0 siblings, 1 reply; 8+ messages in thread
From: David G. Johnston @ 2022-02-03 17:04 UTC (permalink / raw)
To: Christophe Pettus <xof@thebuild.com>; +Cc: Shaozhong SHI <shishaozhong@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>
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:
>
More generally this behavior this documented as "\k"
https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
David J.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-04 12:55 hubert depesz lubaczewski <depesz@depesz.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 0 replies; 8+ messages in thread
From: hubert depesz lubaczewski @ 2022-02-04 12:55 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-04 14:00 Shaozhong SHI <shishaozhong@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Shaozhong SHI @ 2022-02-04 14:00 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Christophe Pettus <xof@thebuild.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>
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:
>>
>
> More generally this behavior this documented as "\k"
>
>
> https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
>
> David J.
>
>
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-04 17:14 Steve Midgley <science@misuse.org>
parent: Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Steve Midgley @ 2022-02-04 17:14 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; Christophe Pettus <xof@thebuild.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-04 17:24 David G. Johnston <david.g.johnston@gmail.com>
parent: Steve Midgley <science@misuse.org>
0 siblings, 1 reply; 8+ messages in thread
From: David G. Johnston @ 2022-02-04 17:24 UTC (permalink / raw)
To: Steve Midgley <science@misuse.org>; +Cc: Shaozhong SHI <shishaozhong@gmail.com>; Christophe Pettus <xof@thebuild.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: How best to work around the issue - regex string cannot contain brackets
@ 2022-02-04 17:29 Steve Midgley <science@misuse.org>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 8+ messages in thread
From: Steve Midgley @ 2022-02-04 17:29 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Shaozhong SHI <shishaozhong@gmail.com>; Christophe Pettus <xof@thebuild.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>
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-...
)
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2022-02-04 17:29 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03 16:53 How best to work around the issue - regex string cannot contain brackets Shaozhong SHI <shishaozhong@gmail.com>
2022-02-03 16:58 ` Christophe Pettus <xof@thebuild.com>
2022-02-03 17:04 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-04 14:00 ` Shaozhong SHI <shishaozhong@gmail.com>
2022-02-04 17:14 ` Steve Midgley <science@misuse.org>
2022-02-04 17:24 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-04 17:29 ` Steve Midgley <science@misuse.org>
2022-02-04 12:55 ` hubert depesz lubaczewski <depesz@depesz.com>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox