具有相同通配符的模式匹配 [英] Pattern matching with identical wildcards
问题描述
我正在使用 PostgreSQL,想知道您是否可以让通配符保留其值.
I'm working with PostgreSQL and want to know whether you can have a wildcard retain its value.
例如说我有
select * from tableOne where field like ‘_DEF_’;
有没有办法让第一个和最后一个通配符完全相同?
Is there a way to get the first and last wildcard to be the exact same character?
因此示例匹配结果可能是:ADEFA 或 ZDEFZ.
So an example matching result could be: ADEFA or ZDEFZ.
推荐答案
您可以使用带有反向引用的正则表达式:
You can use a regular expression with a back-reference:
select *
from some_table
where some_column ~* '^(.)DEF(\1)$'
^(.)DEF(\1)$
表示:开头的某个字符后跟 DEF
后跟第一个字符必须出现在字符串的末尾.
^(.)DEF(\1)$
means: some character at the beginning followed DEF
followed by the first character must occur at the end of the string.
()
定义了一个组,\1
引用了第一个组(在这个例子中是输入序列中的第一个字符)
The ()
defines a group and the \1
references the first group (which is the first character in the input sequence in this example)
SQLFiddle 示例:http://sqlfiddle.com/#!15/d4c4d/1一个>
SQLFiddle example: http://sqlfiddle.com/#!15/d4c4d/1
这篇关于具有相同通配符的模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!