Here’s a simple job for regex.
I’m working on strings built on this model: elem1/elem2@attr (made up of word characters, and / and @ used as separators).
I’m trying to capture the 3 parts : elem1/, elem2, @attr, knowing that a valid string can omit any of the 3 parts (but the separator order must be respected).
I’ve come up with this RegEx:
^(\w+\/)?(\w+)?(@\w+?)$
but it works only if the last part, @attr, is present.
I need a solution that also matches these strings : elem1/, elem2, elem1/elem2.
Any idea?
Your regex should be
^(\w+\/)?(\w+)?(@\w+)?$