Postgres搜索char X但不搜索XX

时间:2022-11-28 21:36:28

Hi I'm trying to find strings in a table that have '=' but not that are '==' in a postgrs table. If I use the following search

嗨我想在一个表中找到'='但在postgrs表中没有'=='的字符串。如果我使用以下搜索

SELECT * FROM someTable where someColumn ~ ' R ';

I find all string with R. But I want to exlude this one that are RR, but if a string has 'something R other RR other' I would it have as result.

我发现所有字符串都是R.但是我想要排除这个RR,但是如果一个字符串有'其他R其他RR其他'我会得到它的结果。

Can you geve me some tips on how to resolve this?

你能告诉我一些如何解决这个问题的技巧吗?

Tank's.

2 个解决方案

#1


1  

You can try and do something like so: SELECT * FROM someTable where someColumn ~ ' R[^R] ';

你可以尝试做类似的事情:SELECT * FROM someTable where someColumn~'R [^ R]';

This should match any string R which is not followed by another R.

这应该匹配任何不跟随另一个R的字符串R.

#2


1  

If you want to use a regex, word boundaries, \y can be used here:

如果你想使用正则表达式,字边界,\ y可以在这里使用:

select * from your_table where s ~ '\yR\y';

See PostgreSQL documentation:

请参阅PostgreSQL文档:

\y    matches only at the beginning or end of a word

\ y仅匹配单词的开头或结尾

See an online test:

查看在线测试:

CREATE TABLE table1
    (s character varying)
;

INSERT INTO table1
    (s)
VALUES
     ('R'),
    ('that are RR'),
    ('that are R')
;
select * from table1 where s ~ '\yR\y';

Output:

    s
1   R
2   that are R

#1


1  

You can try and do something like so: SELECT * FROM someTable where someColumn ~ ' R[^R] ';

你可以尝试做类似的事情:SELECT * FROM someTable where someColumn~'R [^ R]';

This should match any string R which is not followed by another R.

这应该匹配任何不跟随另一个R的字符串R.

#2


1  

If you want to use a regex, word boundaries, \y can be used here:

如果你想使用正则表达式,字边界,\ y可以在这里使用:

select * from your_table where s ~ '\yR\y';

See PostgreSQL documentation:

请参阅PostgreSQL文档:

\y    matches only at the beginning or end of a word

\ y仅匹配单词的开头或结尾

See an online test:

查看在线测试:

CREATE TABLE table1
    (s character varying)
;

INSERT INTO table1
    (s)
VALUES
     ('R'),
    ('that are RR'),
    ('that are R')
;
select * from table1 where s ~ '\yR\y';

Output:

    s
1   R
2   that are R