php - 如何匹配mysql查询中的正则表达式?

标签 php mysql regex postgresql

我有content mysql/postgresql(我正在考虑为项目选择哪一个)数据库中的字段。 2 行示例:

row1: lorem1 <p>author: ABC</p> ipsum author dollar not text
row2: lorem2 author: ipsum author dollar not text

网页用户可以输入who在输入字段中,它必须找到 <p>{who}: (.*?)</p>通过正则表达式。如果用户输入$who = 'author' ,结果将是 ABC仅从第 1 行开始。如何做到这一点?

它会是这样的: SELECT * FROM table WHERE content LIKE '/<p>{$who}: (.*?)<\/p>/s'

最佳答案

在 Postgres 中,您可以使用 regexp_replace() .

示例数据:

create table example (id int, str text);
insert into example values
(1, 'lorem1 <p>author: ABC</p> ipsum author dollar not text'),
(2, 'lorem2 author: ipsum author dollar not text');

查询:

select id, trim(r) result
from 
    example, 
    regexp_replace(str, '^.*?<p>author:(.*?)</p>.*$', '\1') r
where r <> str;

 id | result 
----+--------
  1 | ABC
(1 row) 

关于php - 如何匹配mysql查询中的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252622/

相关文章:

php - RESTful 缓存的工作原理(和使用)

php - SQL 表中的 JSON 得到双结果

mysql - 如何计算工作时间以外的时间

php - 在 MySQL 中连接 3 个表

c# 正则表达式到 javascript

php - WordPress/WooCommerce : cronjob not firing function within class that extends WC_Email

php - 通过curl在youtube视频上发表评论

mysql - SQL优化获取随机Row

regex - 为什么这个简单的 bash 正则表达式不返回 true?

javascript - 正则表达式匹配除空格和最后 4 个字符以外的所有字符