Question about pattern matching

Started by Dimitris Sakellariosover 17 years ago10 messagesgeneral
Jump to latest
#1Dimitris Sakellarios
dimitris.sakellarios@telesuite.gr

TABLENAME

id Candidate pattern
-------------------------
1 0089258068520
2 008925806852
3 00892580685
4 0089258068
5 008925806
6 00892580
7 0089258
8 008925
9 00892
10 0089
11 008
12 00
13 0

PATTERN
-------
0089257000000

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

BR,
Sakellarios Dimitris.

#2Scott Marlowe
scott.marlowe@gmail.com
In reply to: Dimitris Sakellarios (#1)
Re: Question about pattern matching

On Tue, Dec 23, 2008 at 9:05 AM, <dimitris.sakellarios@telesuite.gr> wrote:

TABLENAME

id Candidate pattern
-------------------------
1 0089258068520
2 008925806852
3 00892580685
4 0089258068
5 008925806
6 00892580
7 0089258
8 008925
9 00892
10 0089
11 008
12 00
13 0

PATTERN
-------
0089257000000

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

Without getting out the pgsql manual to write the code, I'd basically
append enough 0s to each candidate to pad them out to the same length,
all ending in one or more 0s, except the longest, which wouldn't need
padding. Then I'd see which one matched.

#3Scott Marlowe
scott.marlowe@gmail.com
In reply to: Scott Marlowe (#2)
Re: Question about pattern matching

I should say I'd pad them to match length with the input value.

#4Craig Ringer
craig@2ndquadrant.com
In reply to: Dimitris Sakellarios (#1)
Re: Question about pattern matching

dimitris.sakellarios@telesuite.gr wrote:

TABLENAME

id Candidate pattern
-------------------------
1 0089258068520
2 008925806852
3 00892580685
4 0089258068
5 008925806
6 00892580
7 0089258
8 008925
9 00892
10 0089
11 008
12 00
13 0

PATTERN
-------
0089257000000

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

The simplest way would be to select the id of the record with
max(length(pattern)), using a WHERE clause to filter for only matching
patterns by comparing the substring of both pattern and teststr of
length min(length(pattern),length(teststr)) for equality.

--
Craig Ringer

#5Julius Tuskenis
julius.tuskenis@gmail.com
In reply to: Dimitris Sakellarios (#1)
Re: Question about pattern matching

Hi, Dimitris

I think simple
SELECT TABLENAME.id
FROM TABLENAME
WHERE prm_patern ilike TABLENAME.candidate_pattern||'%'
ORDER BY |char_length(|TABLENAME.candidate_pattern) desc
LIMIT 1
should do the trick. (Provided TABLENAME is not very large of course)

Julius Tuskenis

dimitris.sakellarios@telesuite.gr rašė:

Show quoted text

TABLENAME

id Candidate pattern
-------------------------
1 0089258068520
2 008925806852
3 00892580685
4 0089258068
5 008925806
6 00892580
7 0089258
8 008925
9 00892
10 0089
11 008
12 00
13 0

PATTERN
-------
0089257000000

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

BR,
Sakellarios Dimitris.

#6Grzegorz Jaśkiewicz
gryzman@gmail.com
In reply to: Dimitris Sakellarios (#1)
Re: Question about pattern matching

SELECT ... from ..... WHERE X LIKE pattern || '%' ORDER BY
length(pattern) DESC LIMIT 1;

???

basic prefix matching for telcos :P

--
GJ

#7Dimitris Sakellarios
dimitris.sakellarios@telesuite.gr
In reply to: Dimitris Sakellarios (#1)
Re: Question about pattern matching

Jef thanks alot for your help.
I appreciate that!
It worked fine.

Dimitris

Quoting "Hoover, Jeffrey" <jhoover@jcvi.org>:

Show quoted text

cameradb_dev=# select id, Candidate_pattern
from all_patterns
where :pattern like Candidate_pattern||'%'
and candidate_pattern between substring(:pattern from 1 for 1) and
:pattern
order by length(Candidate_pattern) desc
limit 1;

id | candidate_pattern
----+-------------------
8 | 008925
(1 row)

note 1: bind (or substitute) your value for :pattern

note 2: "and candidate_pattern between..." only helps
if candidate_pattern is indexed, if there aren't
many rows it is not necessary

-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of
dimitris.sakellarios@telesuite.gr
Sent: Tuesday, December 23, 2008 11:05 AM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Question about pattern matching

TABLENAME

id Candidate pattern
-------------------------
1 0089258068520
2 008925806852
3 00892580685
4 0089258068
5 008925806
6 00892580
7 0089258
8 008925
9 00892
10 0089
11 008
12 00
13 0

PATTERN
-------
0089257000000

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

BR,
Sakellarios Dimitris.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#8Dimitris Sakellarios
dimitris.sakellarios@telesuite.gr
In reply to: Grzegorz Jaśkiewicz (#6)
Re: Question about pattern matching

thanks alot for your help.

Dimitris

Quoting Grzegorz Jaśkiewicz <gryzman@gmail.com>:

Show quoted text

SELECT ... from ..... WHERE X LIKE pattern || '%' ORDER BY
length(pattern) DESC LIMIT 1;

???

basic prefix matching for telcos :P

--
GJ

In reply to: Dimitris Sakellarios (#1)
Re: Question about pattern matching

On Tue, Dec 23, 2008 at 06:05:08PM +0200, dimitris.sakellarios@telesuite.gr wrote:

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

check this:
http://www.depesz.com/index.php/2008/03/04/searching-for-longest-prefix/
read also the comments

additionally, check this url: http://pgfoundry.org/projects/prefix

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz / blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

#10Dimitris Sakellarios
dimitris.sakellarios@telesuite.gr
In reply to: hubert depesz lubaczewski (#9)
Re: Question about pattern matching

Thanks depesz,

It was really helpful

Dimitris.

-----Original Message-----
From: depesz@depesz.com [mailto:depesz@depesz.com]
Sent: Tuesday, December 23, 2008 9:18 PM
To: dimitris.sakellarios@telesuite.gr
Cc: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Question about pattern matching

On Tue, Dec 23, 2008 at 06:05:08PM +0200, dimitris.sakellarios@telesuite.gr
wrote:

QUESTION
--------
Pls let me know which is the best way to match the
following pattern against this table and Receive id = 8
that is the longest matching pattern from left to right.

check this:
http://www.depesz.com/index.php/2008/03/04/searching-for-longest-prefix/
read also the comments

additionally, check this url: http://pgfoundry.org/projects/prefix

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz / blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

__________ NOD32 3693 (20081215) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com