I'm trying to use regex to match any instance of two strings separated by |
, but nothing more.
I've tried ^[^|] \|[^|]$
, but I'm quite a beginner to regex and haven't been able to identify why it doesn't work.
Ex.
String 1|String 2
But the following should not match:
String 1|String 2|String 3
String 1
CodePudding user response:
As Wiktor pointed out, your regex is missing a
before the $
. Actually the regex you have in your question would not match any of the above three inputs without either adding the
or removing the $
. Here would be the proper regex:
-
CodePudding user response:
Use the pattern
/^(?=.*\|)(?!.*\|.*\|).*$/gm
The part
(?=.*\|)
says there is some (at least one)|
present.The part
(?!.*\|.*\|)
says there are not 2 or more|
present.Test your input here.