I've got a regex that I'm trying to use to detect if a certain input is valid. The syntax of the input should be {A|B|C}
. {A|B|}
should fail.
(?:
(
\{{1}
(?:[A-Z0-1-_.*]+ \| [A-Z0-1-_.*]+)*
\}{1}
)
)
This is what I have so far, but I'm starting to think this isn't the way to go. Even if it did work properly, it wouldn't allow {A}
which should be valid.
So basically what I'm trying to do is check if each [A-Z0-1-_.*]
element is split by |
and that there are no empty elements within the {}
brackets.
One concept I'm really struggling with which feels relevant here is having n amount of possible elements. Like let's say, the string to validate is Foo{A}Bar{B|C}Test
The way I would check that has 2 elements. One element to check for alphabetical characters, and another element to check the bracketed characters.
So to check the string above, I would do alphaElem*|BracketElem*|alphaElem*|BracketElem*|alphaElem*
But that's a lot of writing out, and it doesn't scale if the amount of elements increases. Is there some way I can solve this with regex?