正则表达式匹配除引号之间的单词之外的所有内容 [英] Regex match everything except words between quotes

查看:95
本文介绍了正则表达式匹配除引号之间的单词之外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个正则表达式,它匹配除引号之间的单词之外的所有内容.例如:

I want to write a regex what matches everything except words between quotes. Ex.:

 Lorem ipsum "dolor" sit amet, consectetur "adipiscing" elit.
 Nunc ultrices varius odio, "ut accumsan nisi" aliquet vitae.
 "Ut faucibus augue tortor, at aliquam purus dignissim eget."

所以我想要一个匹配以下字符串的正则表达式:

So I want a regex what matches the following strings:

  • Lorem ipsum
  • 坐下来,consectetur
  • 精英.Nunc ultrices varius odio,
  • 个人简历.

我只有以下表达式匹配引号内的子字符串:

I only have the following expression that matches substrings inside quotes:

([\"'])(?:\\\1|.)*?\1

推荐答案

如果你正在使用 PCRE,你可以使用

If you are using PCRE, you may use

([\"'])(?:\\.|(?!\1)[^\\])*?\1(*SKIP)(*F)|(?:[^\\"']|\\.)+

查看其演示.

详情

  • ([\"'])(?:\\.|(?!\1)[^\\])*?\1 - 一个 "..."'...' 带有转义引号支持的子字符串:
    • ([\"']) - 第 1 组(用 \1 引用):一个 "'
    • (?:\\.|(?!\1)[^\\])*? - 出现 0+ 次(尽可能少,因为 *? 懒惰)的:
      • \\. - 一个转义序列
      • | - 或
      • (?!\1)[^\\] - 除了 \ 和组 1 中的引号字符之外的任何字符
      • ([\"'])(?:\\.|(?!\1)[^\\])*?\1 - a "..." or '...' substring with escaped quote support:
        • ([\"']) - Group 1 (referred to with \1): a " or '
        • (?:\\.|(?!\1)[^\\])*? - 0+ occurrences (as few as possible due to *? being lazy) of:
          • \\. - an escape sequence
          • | - or
          • (?!\1)[^\\] - any char other than \ and the quote char in Group 1
          • [^\\"'] - 除了 \'" 之外的字符立>
          • \\. - 一个转义序列.
          • [^\\"'] - a char other than \, ' or "
          • \\. - an escape sequence.

          这篇关于正则表达式匹配除引号之间的单词之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆