允许在postgreSQL正则表达式文本中使用括号
这些句子有用允许在postgreSQL正则表达式文本中使用括号,sql,regex,postgresql,Sql,Regex,Postgresql,这些句子有用 SELECT (regexp_matches('Euroschinus Hoff+300'::text, E'(Euroschinus Hoff[\+])([0- 9]+)'::text)::text[])[1]::text as counter select array_scientificname from simple_cal where array_scientificname ~ 'Semecarpus' 但是,如果有一些括号,不管在文本中的什么地方,这两个都不起作用
SELECT (regexp_matches('Euroschinus Hoff+300'::text, E'(Euroschinus Hoff[\+])([0- 9]+)'::text)::text[])[1]::text as counter
select array_scientificname from simple_cal where array_scientificname ~ 'Semecarpus'
但是,如果有一些括号,不管在文本中的什么地方,这两个都不起作用
SELECT (regexp_matches('Euroschinus (testing) Hoff+300'::text, E'(Euroschinus (testing) Hoff[\+])([0-9]+)'::text)::text[])[1]::text as counter
select array_scientificname from simple_cal where array_scientificname ~ 'Semecarpus(test)'
我只是想得到,文本。没有为()定义模式,可以在文本的任何位置
我注意到,在括号之前使用\会起作用(见下文),但这根本不实用。我想我应该在字符串中包含()允许的地方
SELECT (regexp_matches('Euroschinus (testing) Hoff+300'::text, E'(Euroschinus jaffrei \\(testing\\) Hoff[\+])([0-9]+)'::text)::text[])[1]::text as counter
这不会返回任何内容:
SELECT (regexp_matches(
'Euroschinus (testing) Hoff+300'::text
, E'(Euroschinus jaffrei \\(testing\\) Hoff[\\+])([0-9]+)')::text[])[1]::text;
这将在从模式中删除字符串jaffrei
后:
SELECT (regexp_matches(
'Euroschinus (testing) Hoff+300'::text
, E'(Euroschinus \\(testing\\) Hoff[\\+])([0-9]+)')::text[]);[1]::text
简化regexp,释放无意义的字符类:
SELECT (regexp_matches(
'Euroschinus (testing) Hoff+300'::text
, E'(Euroschinus \\(testing\\) Hoff\\+)([0-9]+)')::text[])[1]::text;
如果必须添加反斜杠,请尝试设置(自PostgreSQL 9.1以来的默认设置),并使用普通字符串而不是Posix转义序列:
SELECT (regexp_matches(
'Euroschinus (testing) Hoff+300'::text
, '(Euroschinus \(testing\) Hoff\+)([0-9]+)')::text[])[1]::text;
但是,如果您只对第一次点击感兴趣,那么最好首先使用substring()
。捕获括号选择所需的字符串:
SELECT substring('Euroschinus (testing) Hoff+300'
, '(Euroschinus \(testing\) Hoff\+)[0-9]+');
最后,如果您对字符串(?)
中存在的()
感到困扰,请删除它们:
SELECT substring(translate('Euroschinus (testing) Hoff+300', '()', '')
, '(Euroschinus testing Hoff\+)[0-9]+');
1.
jaffrei
word会损坏您的regexp 2。我不明白-你想先提取数字还是文本计数器
建议使用第一个,[1]
建议使用第二个。该问题在当前状态下尚不清楚。请添加一个字符串示例以及您希望从中获得的内容。好的,感谢您的回答,我发现在最终select语句之前,无法避免以某种方式编辑文本(添加\\或)(事实上,我在这里给出的示例只是更复杂句子的一部分)。我希望正则表达式中有一些选项可以避免。。。谢谢