我有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/