字符类和特殊条件在某些其他条件下的正则表达式匹配

发布于 2025-01-12 02:33:59 字数 489 浏览 2 评论 0原文

我想匹配包含某些重复字符的字符串部分,以及仅在给定特定条件的情况下的某些其他字符。例如,仅当数字前面有一个加号时,才匹配尖括号中包含的字符 az 和数字。

abcde 匹配。

不应匹配任何内容。

abcde+1 匹配 将 匹配到 abcde+1asd+2+3+4as

不应该匹配任何东西。

我尝试过的正则表达式是 <([az]|(\+(?=[0-9])|[0-9](?<=[\+])))*>;

I want to match a section of a string that contains certain characters repeated, along with certain other characters only given a certain criteria. For instance matching characters a-z contained in angle brackets and numbers only if the number is preceeded by a plus.

Matching <abcde> to abcde.

<abcde1> should not match anything.

Matching <abcde+1> to abcde+1
Matching <abcde+1asd+2+3+4as> to abcde+1asd+2+3+4as

<abcde+> should not match anything.

The regex I've tried is <([a-z]|(\+(?=[0-9])|[0-9](?<=[\+])))*>.

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

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

发布评论

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

评论(1

み零 2025-01-19 02:33:59

您可以使用

(?<=<)(?:[a-zA-Z]+(?:\+\d+)*)+[a-zA-Z]*(?=>)
<((?:[a-zA-Z]+(?:\+\d+)*)+[a-zA-Z]*)>

查看正则表达式演示详细信息

  • (?<=<) - 正向后查找,需要紧邻左侧的 < 字符
  • (? :[a-zA-Z]+(?:\+\d+)*)+ - 出现一次或多次
    • [a-zA-Z]+ - 一个或多个字母
    • (?:\+\d+)* - 零个或多个 + 序列以及一个或多个数字
  • [a-zA-Z]* - 一个或多个 ASCII 字母
  • (?=>) - 需要 > 的正向预测; char 紧邻右侧。

You can use

(?<=<)(?:[a-zA-Z]+(?:\+\d+)*)+[a-zA-Z]*(?=>)
<((?:[a-zA-Z]+(?:\+\d+)*)+[a-zA-Z]*)>

See the regex demo. Details:

  • (?<=<) - a positive lookbehind that requires a < char immediately on the left
  • (?:[a-zA-Z]+(?:\+\d+)*)+ - one or more occurrences of
    • [a-zA-Z]+ - one or more letters
    • (?:\+\d+)* - zero or more sequences of + and one or more digits
  • [a-zA-Z]* - one or more ASCII letters
  • (?=>) - a positive lookahead that requires a > char immediately on the right.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文