postgresql Postgres 正则表达式 ^ 以 $ 开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46978821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:39:23  来源:igfitidea点击:

Postgres regex ^ begins with and $ ends with

sqlregexpostgresql

提问by moonshot

Newbie question. I'm looking for stings beginning with letter 'R' this works:

新手问题。我正在寻找以字母“R”开头的刺痛,这有效:

SELECT *
FROM table
WHERE column LIKE  'R%'

this does not

这不

SELECT *
FROM table
WHERE column LIKE '^R%'

WHY? Also just curious, if I want it to end with R, I use the $?

为什么?也只是好奇,如果我想以 结尾R,我使用$?

'%R$'

Pattern matching is not explained well here. If you know any other resources for a layman please share. thanks https://www.postgresql.org/docs/9.3/static/functions-matching.html

这里没有很好地解释模式匹配。如果您知道外行的任何其他资源,请分享。谢谢 https://www.postgresql.org/docs/9.3/static/functions-matching.html

回答by Dan

The manual is very clear LIKEand regular expressions are two different things.

手册很清楚LIKE,正则表达式是两个不同的东西。

LIKE

喜欢

Accordingly to the manual:

根据手册:

The LIKE expression returns trueif the string matches the supplied pattern. (As expected, the NOT LIKE expression returns falseif LIKE returns true, and vice versa. An equivalent expression is NOT (string LIKE pattern).)

如果字符串与提供的模式匹配,则LIKE 表达式返回true。(正如预期的那样,如果 LIKE 返回 true ,则 NOT LIKE 表达式返回false,反之亦然。等效的表达式是 NOT(字符串 LIKE 模式)。)

So, LIKE returns true or false while matching a string.

因此,LIKE 在匹配字符串时返回 true 或 false。

%is similar to *in filesearch, it means it will match zero or any after or before.

%类似于在文件搜索*中,这意味着它将匹配零或任何之后或之前。

  • R%R and any character after.
  • %RAny character befor and R
  • R%R 和之后的任何字符。
  • %R和 R 之前的任何字符

LIKEit's used instead of =, and more functionalities are explained in the manual.

LIKE它用于代替=,手册中解释了更多功能。

-- Gets zero or any characters before one R
SELECT * FROM table WHERE column LIKE '%R'

-- Gets zero or any characters after one R
SELECT * FROM table WHERE column LIKE 'R%'

ILIKE

我喜欢

LIKEis case sensitive, to make it case insensitive you can use ILIKE

LIKE区分大小写,要使其不区分大小写,您可以使用 ILIKE

REGULAR EXPRESION

正则表达式

In postgreSQL, for a regular expression match, instead of using =you use ~and the regular expression format is accordingly to the POSIX regular expressionsstandards

在 postgreSQL 中,对于正则表达式匹配,而不是使用=您使用~并且正则表达式格式相应于POSIX 正则表达式标准

An example is:

一个例子是:

-- Gets zero or any spaces numbers and text characters before one R
SELECT * FROM table WHERE column ~ '^[\s\w]*[R]{1}$'

-- Gets zero or any spaces numbers and text characters after one R
SELECT * FROM table WHERE column ~ '^[R]{1}[\s\w]*$'

In the manual there's the explanation for all the available operators for regular expressions. How to use regular expressions is other thing and it is in the POSIX regular expressionsstandards; that has nothing to do with PostgreSQL.

在手册中有对所有可用的正则表达式运算符的解释。如何使用正则表达式是另一回事,它在POSIX 正则表达式标准中;这与 PostgreSQL 无关。