简体   繁体   English

regexp_substr:在给定模式之前查找文本

[英]regexp_substr: find the text before a given patern

I want to find the text before a given pattern. 我想在给定模式之前找到文本。 My main problem is when my text has many rows. 我的主要问题是我的文本有很多行。

Here the example: 这里的例子:

    SQL> with foo as
  2  (select '1 first test error log blabla ' k from dual
  3  union
  4  select '2 second test
  5  zz error log blablabla ' k from dual
  6  )
  7  SELECT REGEXP_SUBSTR(k,'.*error log',1,1) AS result_
  8         ,k from foo;
RESULT_                               K
------------------------------------- -------------------------------------
1 first test error log                1 first test error log blabla
zz error log                          2 second test
                                      zz error log blablabla

The result is false for the second rows. 第二行的结果为false。 It should be: 它应该是:

RESULT_                               K
------------------------------------- -------------------------------------
1 first test error log                1 first test error log blabla
2 second test                         2 second test
zz error log                          zz error log blablabla

I run this query on a 11.2 Oracle database 我在11.2 Oracle数据库上运行此查询

From the Oracle documentation , the 5th argument to REGEXP_SUBSTR is: Oracle文档中REGEXP_SUBSTR的第五个参数是:

match_parameter is a text literal that lets you change the default matching behavior of the function. match_parameter是文本文字,可让您更改函数的默认匹配行为。

Which has the options : 可以选择

  • 'n' allows the period ( . ), which is the match-any-character character, to match the newline character. 'n'允许句点( . )(它是任何字符的匹配字符)与换行符匹配。 If you omit this parameter, then the period does not match the newline character. 如果省略此参数,则句点与换行符不匹配。

SQL Fiddle SQL小提琴

Oracle 11g R2 Schema Setup : Oracle 11g R2架构设置

CREATE TABLE table_name ( k ) AS
SELECT '1 first test error log blabla ' FROM DUAL UNION ALL
SELECT '2 second test
zz error log blablabla ' FROM DUAL;

Query 1 : 查询1

SELECT REGEXP_SUBSTR(k,'.*error log',1,1,'n') AS result_,
       k
FROM   table_name

Results : 结果

|                RESULT_ |                              K |
|------------------------|--------------------------------|
| 1 first test error log | 1 first test error log blabla  |
| 2 second test          | 2 second test                  |
| zz error log           | zz error log blablabla         |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM