Thread
-
How best to work around the issue - regex string cannot contain brackets
Shaozhong SHI <shishaozhong@gmail.com> — 2022-02-03T16:53:18Z
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 <xof@thebuild.com> — 2022-02-03T16:58:45Z
> 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 <david.g.johnston@gmail.com> — 2022-02-03T17:04:42Z
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. -
Re: How best to work around the issue - regex string cannot contain brackets
hubert depesz lubaczewski <depesz@depesz.com> — 2022-02-04T12:55:58Z
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 <shishaozhong@gmail.com> — 2022-02-04T14:00:55Z
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. > > -
Re: How best to work around the issue - regex string cannot contain brackets
Steve Midgley <science@misuse.org> — 2022-02-04T17:14:39Z
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 <david.g.johnston@gmail.com> — 2022-02-04T17:24:24Z
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 <science@misuse.org> — 2022-02-04T17:29:53Z
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 )