PostgreSQL:如何在Where子句中使用RegexP?

发布于 2025-02-04 02:16:51 字数 920 浏览 0 评论 0原文

我有一些记录,例如以下:

乌拉坦desc
1432aman032-枪支
1433benang156 -sikat pintu
1434oli bell One at -d 20w40
1435水refill
1436kabur001 -gosok air
1437kabel ties 20 cm -50,

基于上面的桌子,我有我有两个,我有我有两个。字段中的字符串格式desc:

  1. 正确的格式,即Urutan 1432,1433,1436。
  2. 不正确的格式,即Urutan 1434,1435,1437,

如您所见,正确的格式在下面具有类似的格式:

aman032 - 枪支

然后我拆分:

[aman] [032] - [枪支]

[word] [3 digits number] [space] [ - ] [ - ] [ - ] [SPACE] [任何单词]

我的目标是要选择所有与POSGRESQL REGEXP在Where子句中不匹配正确格式的记录(在我不知道哪个Urutan的情况下)。因此,上表的结果是Urutan 1434,1435,1437

I have some records like below:

urutandesc
1432AMAN032 - Gunting
1433BENANG156 - Sikat Pintu
1434Oli Bell One AT-D 20W40
1435Water Refill
1436KABUR001 - Gosok Air
1437Kabel Ties 20 Cm - 50

Based on the table above, i have two formats of string in field desc:

  1. the correct format, which are urutan 1432, 1433, 1436.
  2. the incorrect format, which are urutan 1434, 1435, 1437

as you can see, the correct format has format like this below:

AMAN032 - Gunting

then i split:

[AMAN][032] - [Gunting]

[a word][3 digits number][space][-][space][any words].

my goal is I want to SELECT all records which does not match the correct format using PosgreSQL REGEXP in WHERE Clause (in a condition I don't know which urutan). so the result from table above are urutan 1434, 1435, 1437.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-02-11 02:16:51

尝试以下查询:

SELECT * 
FROM tab
WHERE NOT desc_ ~ '^[A-Za-z]+[0-9]{3} - [A-zA-Z ]+

REGEX说明:

  • ^:String的启动
  • [A-ZA-Z]+:任何字母顺序的字符
  • [0-9] {3 } :三位数
  • - :space + dash + space
  • [a-za-z] + :字母字符和空格
  • $ <$ < 的任何组合/code>:字符串的结尾

尝试在这里

注意:此正则孔中的孔可能是字母字符的最后一个组合。您可以在提供的链接上使用它以包含您想要/不想匹配的字符,或者分享有关该部分如何组成的更多详细信息,我可以尝试改进模式匹配。

REGEX说明:

  • ^:String的启动
  • [A-ZA-Z]+:任何字母顺序的字符
  • [0-9] {3 }:三位数
  • -:space + dash + space
  • [a-za-z] +:字母字符和空格
  • $ <$ < 的任何组合/code>:字符串的结尾

尝试在这里

注意:此正则孔中的孔可能是字母字符的最后一个组合。您可以在提供的链接上使用它以包含您想要/不想匹配的字符,或者分享有关该部分如何组成的更多详细信息,我可以尝试改进模式匹配。

Try with the following query:

SELECT * 
FROM tab
WHERE NOT desc_ ~ '^[A-Za-z]+[0-9]{3} - [A-zA-Z ]+

Regex Explanation:

  • ^: start of string
  • [A-Za-z]+: any alphabetical character
  • [0-9]{3}: three digits
  • -: space + dash + space
  • [A-zA-Z ]+: any combination of alphabetical character and space
  • $: end of string

Try it here.

Note: The hole in this regex may be in the last combination of alphabetical characters. You can either play with it at the provided link to include or exclude characters that you want/don't want to match for that part, or share further details on how that part can be composed of, and I could try improving the pattern matching.

Regex Explanation:

  • ^: start of string
  • [A-Za-z]+: any alphabetical character
  • [0-9]{3}: three digits
  • -: space + dash + space
  • [A-zA-Z ]+: any combination of alphabetical character and space
  • $: end of string

Try it here.

Note: The hole in this regex may be in the last combination of alphabetical characters. You can either play with it at the provided link to include or exclude characters that you want/don't want to match for that part, or share further details on how that part can be composed of, and I could try improving the pattern matching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文