0
votes

I have a string with two possible out comes it will either be divided by &| or just | is there a way of doing an or expression to determine which of these is true in regex?

1
Make & optional &?\| ? - HamZa

1 Answers

3
votes

I take it you mean that you wish to match strings with either &| or | and then, in the case of a match, check which case it was that matched?

If so, you can do by matching with a regex in which & is optional -- as suggested by HamZa in a comment on the question -- and using re-find or capture groups to capture the matching part of the string which you can then examine:

(re-find #"&?\|" "asdf|adsf")
;= "|"

(re-find #"&?\|" "asdf&|asdf")
;= "&|"

With capture groups (use peek to get at a vector's final element):

(re-matches #".*[^&](&?\|).*" "asdf&|asdf")
;= ["asdf&|asdf" "&|"]