Reputation: 1001
I am trying to make a regex for the endpoint which can validate a string for lowercase (or concatenated with -
)
BUT if there is a curly braces, then a word inside a curly braces should be in lowerCamelCase.
Here is an example string:
/v1/accounts/{accountNumber}/balances
OR /v1/user-account/balances-sheet
I am using a following regex '^[\/a-z\{\}]+$'
to validate above examples but not getting the point how I can validate curly braces condition OR if it possible to combine in single Regex?
Upvotes: 1
Views: 91
Reputation: 424983
Try this:
^(\/([\da-z]+(-[\da-z]+)?|\{[a-z]+([A-Z][a-z]+)*}))+$
See live demo.
Upvotes: 1
Reputation: 292
The regex to match both of your examples would be ^([a-z0-9\/\-]+)(\{(?<Token>[\w\/]+)\})?([a-z0-9\/\-]+)?$
-- check it on regex101
Upvotes: 1
Reputation: 784998
You may use this regex to validate both cases:
^(?:\/\{[a-z]+(?:[A-Z][a-z]*)*\}|\/\w+(?:-\w+)*)+$
RegEx Breakup:
^
: Start(?:
: Start non-capture group
\/\{[a-z]+(?:[A-Z][a-z]*)*\}
: Match /
followed by variable name that would be {<lowerCamelCase>}
|
: OR\/\w+(?:-\w+)*
: Match /
followed by valid path component that may have 0 or more hyphen separated words)+
: End non-capture group. Repeat this group 1+ times$
: EndUpvotes: 1