当前位置:网站首页>Pattern matching and regular expressions in PostgreSQL - Das
Pattern matching and regular expressions in PostgreSQL - Das
2022-07-02 13:39:00 【Jiedao jdon】
In this paper , I will discuss PostgreSQL Pattern matching and regular expressions in .
An important aspect of writing queries is that users should be able to filter and select the data that the system needs to access . Additional or unnecessary data will increase bandwidth and reduce query performance . therefore ,SQL Provides a filter predicate “WHERE”, Users can use it to filter queries and select results that match only filter predicates .
PostgreSQL Regular expressions in
In the normal SQL In case of operation ,LIKE Operators are good , But when filtering large databases ,LIKE Operator seems to have some performance problems . Besides , Use LIKE The filter condition of the operator is limited to including wildcards (%) To find patterns .
To overcome this problem ,PostgreSQL Provides a high-level method of pattern matching using regular expressions . For a long time , Regular expressions are widely used in programming languages , However , stay SQL Use these regular expressions in the statement , Make the query highly dynamic , And perform better in large databases .
PostgreSQL The regular expression in is using TILDE ( ~ ) Operator , And use '.* ” As a wildcard operator .
SELECT * from GreaterManchesterCrime WHERE CrimeID = '^.*$'; |
As you can see , We are PostgreSQL Regular expressions are used in , Used TILDE(~) Operators and wildcards '.*'. This query will start from GreaterManchesterCrime Select all in the table that have valid CrimeID The record of . Because the mode condition is only a wildcard , It will get all records from the table .
in addition , stay PostgreSQL Another thing to note when writing regular expressions in is , Pattern matching statements always begin with "^" Operator starts , With "$" End of symbol . These two operators mark the beginning and end of a regular expression statement . Summarize the following figure , When using wildcards to filter all records , The expression can be implemented as "^.*$".
Regular expressions - A string beginning with a number or character
In the last section , We have learned how to implement regular expressions by using wildcards . Now? , We will take a step forward , Try for column CrimeID Get records that begin with characters or numbers .
stay PostgreSQL in , have access to "[a-z]" or "[A-Z]" Pattern to match alphanumeric characters , It depends on what we are trying to match . One thing to note here is that , because PostgreSQL It's case sensitive , therefore , The exact case we want to match must be specified in the pattern .
similarly , For matching numbers , We can use "[0-9]" or "/d". Now we use Regex Filter characters and numbers and have some understanding , Let's continue to implement it in the database .
For matching characters -
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ '^[AZ].*$' |
CrimeID The field has been filtered , Contains only ID Records beginning with capital letters . Please note that wildcards are used again “ .* ” To express SQL Anything after the first character in the statement .
To filter numbers in statements , We can simply write the following query :
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ '^[0–9].*$' |
Only those beginning with numbers CrimeID Records of are filtered to match regular expression conditions .
Regular expressions —— String starting with repeated numbers or characters
In the previous section of this article , We have learned how to be in PostgreSQL Using regular expression syntax SQL Inquire about , And how to filter characters and numbers in the first position of the filtered string .
Now? , Let's add some more complex content to the requirements , And filter records that start with multiple characters or numbers .PostgreSQL Just provide the count in curly brackets , You can easily specify the number of repetitions . perhaps , You can also repeat the same character pattern matching multiple times to match your criteria . for example , We want to filter all records that start with two characters . This query can be written as follows .
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ '^[AZ] [AZ].*$' |
perhaps
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ '^[AZ] {2}.*$'
|
We use numbers in curly braces to indicate the number of times to find patterns in the column . This makes the query very dynamic , Because you can specify any number of characters to search in the query , You will get results .
Again , You can also implement the same logic for repeated values . under these circumstances , Queries can be written as follows .
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ '^[0–9]{2}.*$'
|
This shows the implementation of pattern matching using regular expressions while filtering duplicate values .
Regular expressions —— String starting with characters and numbers
In the last part of this article , We will combine what we have seen so far . We will build queries and match patterns that will filter records that begin with characters and are followed by numbers . To form a standard query , You can combine the conditions of alphabetic and numeric characters in regular expressions .SQL The statement can be written as follows .
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ '^[AZ][0–9].*$' |
边栏推荐
- Don't spend money, spend an hour to build your own blog website
- Student course selection information management system based on ssm+jsp framework [source code + database]
- P3807 [template] Lucas theorem /lucas theorem
- The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
- Pocket Raider comments
- Engineers who can't read device manuals are not good cooks
- [technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
- Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
- 文件的下载与图片的预览
- Unity skframework framework (XVI), package manager development kit Manager
猜你喜欢

mac(macos Monterey12.2 m1) 个人使用php开发

Runhe hi3516 development board openharmony small system and standard system burning

Why is the default of switch followed by break?

不会看器件手册的工程师不是个好厨子

OpenFOAM:lduMatrix&lduAddressing

Unity SKFramework框架(十三)、Question 问题模块

Unity skframework framework (XVII), freecameracontroller God view / free view camera control script

2022零代码/低代码开发白皮书【伙伴云出品】附下载

EasyDSS点播服务分享时间出错如何修改?

Unity skframework framework (XVI), package manager development kit Manager
随机推荐
Unity SKFramework框架(十五)、Singleton 单例
Mysql常用命令详细大全
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
Skillfully use SSH to get through the Internet restrictions
ArrayList and LinkedList
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
文件的下载与图片的预览
混沌工程平台 ChaosBlade-Box 新版重磅发布
Embedded software development
Three methods of finding LCA of the nearest common ancestor
Dingtalk 发送消息
2022 Heilongjiang provincial examination on the writing skills of Application Essays
最近公共祖先LCA的三种求法
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
EasyDSS点播服务分享时间出错如何修改?
【youcans 的图像处理学习课】总目录
JS逆向之巨量创意signature签名
JS逆向之行行查data解密
伙伴云表格强势升级!Pro版,更非凡!
Bridge of undirected graph