字符类和特殊条件在某些其他条件下的正则表达式匹配
我想匹配包含某些重复字符的字符串部分,以及仅在给定特定条件的情况下的某些其他字符。例如,仅当数字前面有一个加号时,才匹配尖括号中包含的字符 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
查看正则表达式演示。 详细信息:
(?<=<)
- 正向后查找,需要紧邻左侧的<
字符(? :[a-zA-Z]+(?:\+\d+)*)+
- 出现一次或多次[a-zA-Z]+
- 一个或多个字母(?:\+\d+)*
- 零个或多个+
序列以及一个或多个数字[a-zA-Z]*
- 一个或多个 ASCII 字母(?=>)
- 需要> 的正向预测;
char 紧邻右侧。You can use
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.