简体   繁体   English

正则表达式匹配一堆字符+仅一个|

[英]Regex to Match a bunch of character + only a single |

I have a regex that works great for matching the following.. 我有一个正则表达式非常适合匹配以下内容。

^[a-z-&+/]

Now I want add in a match for the "|" 现在,我要为“ |”添加一个匹配项 symbol, but only if it starts with a single "|" 符号,但前提是它以单个“ |”开头

True results 真实结果

|hello
apple
test||test

False Results 错误的结果

||hello

I tried a bunch of ways on regex101.com - but I can't come up with the right regex. 我在regex101.com上尝试了多种方法-但我无法提出正确的regex。

This regex works for what you want: 此正则表达式适用于您想要的:

^([a-z-&+\/]|\|(?!\|))

This goes from the start of the input, tries to find any of your original characters ( [az-&+/] ) OR a | 这从输入的开头开始,尝试查找您的任何原始字符( [az-&+/] )或| not followed by a | 后面没有| (using a negative lookahead). (使用负前瞻)。

See a demo on regex101.com . 参见regex101.com上的演示

This worked for me: 这为我工作:

                      /^[|a-z-&+/](?!\|)/

One can use | 一个可以使用 inside character set as well. 内部字符集。 Check http://www.regexr.com/3coad where i tested the match. 检查http://www.regexr.com/3coad我在哪里测试了比赛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM