正则表达式优化:如何不在搜索中的带引号的子字符串中包含引号

发布于 2024-09-27 04:17:30 字数 573 浏览 2 评论 0原文

我编写了一个正则表达式来将搜索字符串拆分为其组成部分。功能包括:

  • 运算符:+-ANDOR
  • 按引号进行单词分组(目前为单引号和双引号) )
  • 正确忽略撇号

那么:

((?<=^|\s)(?:[\+\-]?"[^"]+"(?=\s|$)|[\+\-]?'[^']+'(?=\s|$)|[\+\-]?\S+|AND|and|OR|or)(?=$|\s))

从结果匹配中排除分隔符引号的最简单方法是什么?示例:

lsdkjflws's ldkj and "lfldkfjs's ldkjfls" lskdj

结果如下:

  • lsdkjflws's
  • ldkj
  • "lfldkfjs's ldkjfls"
  • lskdj

我不需要这样做,我只想在正则表达式中再完成一步。

I've written a regex to split a search string into its component parts. Features include:

  • Operators: +, -, AND, OR
  • Word grouping by quotes (single and double for now)
  • Correctly ignoring apostrophes

So:

((?<=^|\s)(?:[\+\-]?"[^"]+"(?=\s|$)|[\+\-]?'[^']+'(?=\s|$)|[\+\-]?\S+|AND|and|OR|or)(?=$|\s))

What is the easiest way to exclude the delimiter quotes from the result matches? Example:

lsdkjflws's ldkj and "lfldkfjs's ldkjfls" lskdj

results in these pieces:

  • lsdkjflws's
  • ldkj
  • and
  • "lfldkfjs's ldkjfls"
  • lskdj

I don't need to do this, I'd just like to accomplish one more step in the regex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2024-10-04 04:17:31

什么发动机?如果它支持负向和正向前瞻,那就很简单:

而不是这些:

"[^"]+"

您将使用类似这样的内容:

(?<=")[^"]+(?=")

然后,这会从匹配中排除引号,但仍然只匹配引号的内容。我希望这就是你所追求的。

What engine? If it supports negative and positive lookahead, it's easy:

Instead of these:

"[^"]+"

You would use something like this:

(?<=")[^"]+(?=")

This then excludes the quotes from the match, but still only matches the content of the quotes. I hope this is what you're after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文