简体   繁体   English

正则表达式:正则表达式匹配除特定模式之外的所有内容

[英]Regex: regex match everything but specific pattern

I need a regex that finds values other than !#!我需要一个查找!#!以外的值的正则表达式in a string.在一个字符串中。

If, for example, the string were:例如,如果字符串是:

Text1!#!Text2!#!Text#3!#!

it should return Text1 , Text2 , Text#3它应该返回Text1Text2Text#3

[^!#!] would match the # in Text#3 as well, which I don't want. [^!#!]也会匹配Text#3中的# ,这是我不想要的。

You could solve this problem using positive lookbehind and lookahead.您可以使用积极的后视和前瞻来解决这个问题。

Simply:简单地:

  • match anyhting (.+?)匹配任何东西(.+?)
  • being at the beginning of the text or having the pattern in front (?<=^|!#!)在文本的开头或前面有模式(?<=^|!#!)
  • and being at the end of the text or having the pattern behind (?=!#!|$)并且在文本的末尾或后面有模式(?=!#!|$)

See your example here: https://rubular.com/r/f6BDr9CxeaQTIz using (?<=^|.#?)(?+?)(?=!#!|$)在此处查看您的示例: https://rubular.com/r/f6BDr9CxeaQTIz使用(?<=^|.#?)(?+?)(?=!#!|$)

You can use REGEXP_SUBSTR in conjunction with CONNECT_BY to split the string into words separated by !#!您可以将REGEXP_SUBSTRCONNECT_BY结合使用,将字符串拆分为由!#! . . We use the regex:我们使用正则表达式:

(.*?)(!#!|$)

which matches some number of characters lazily until it encounters either !#!它会延迟匹配一些字符,直到遇到!#! or the end of string ( $ ).或字符串的结尾 ( $ )。

For example:例如:

SELECT REGEXP_SUBSTR ('Text1!#!Text2!#!Text#3!#!',
                      '(.*?)(!#!|$)',
                      1,
                      LEVEL,
                      '',
                      1)
              AS VAL
FROM DUAL
CONNECT BY REGEXP_SUBSTR ('Text1!#!Text2!#!Text#3!#!',
                          '(.*?)(!#!|$)',
                          1,
                          LEVEL,
                          '',
                          1)
IS NOT NULL

Output: Output:

VAL
Text1
Text2
Text#3

Demo on dbfiddle dbfiddle 上的演示

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

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