正则表达式解析器和自定义转义字符

发布于 2024-12-09 07:21:10 字数 428 浏览 4 评论 0原文

我有一个字符串,我想从该字符串的开头和结尾中挑选出带有加号的子字符串。

示例:

text +name+ filler with /+ sign +b+ bold text +/b+

我想挑选 +name+ +b++/b+,但我当前的正则表达式会处理 + 符号 + 作为可能的值”

这是我正在使用的正则表达式 \+[\-@\w\s\d\/\!]*\+

我已经尝试过在前面添加一个[^/],但这会添加 + 之前的任何字符,并且无法处理 +n++b+

我正在尝试找出环视和环视,只是不知道如何应用它。

I have a string that I'd like to pick out sub-strings from that start and end with a plus sign.

example:

text +name+ filler with /+ sign +b+ bold text +/b+

I'd like to pick out the +name+ +b+and the+/b+, but my current regex would treat + sign + as a possible value"

this is the regex I am using \+[\-@\w\s\d\/\!]*\+

I've tried adding a [^/] in front, but that adds whatever character there was before the + and cannot deal with +n++b+

I'm trying to figure out the lookaround and lookbehind, just not sure how to apply it.

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

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

发布评论

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

评论(1

萌化 2024-12-16 07:21:10

负向回顾看起来像这样 (?。仅当前一个字符不是 / 时才匹配。

示例:

(?<!/)\+[^+]*\+

注意:

  • 如果您使用 / 字符作为正则表达式的分隔符,则可能必须转义该字符。
  • 并非所有正则表达式引擎都支持lookbehind。

The negative lookbehind looks like this (?<!/). This only matches if the previous character is not a /.

Example:

(?<!/)\+[^+]*\+

Note:

  • You may have to escape the / if you are using that character as the delimiter for your regular expression.
  • Not all regular expression engines suport lookbehinds.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文