If you need to ensure that all required digits match:
"^(\\d{3}\\/\\d{2}\\-\\d{3})$"
If you require 123/12 and -125 is optional
"^(\\d+\\/\\d+)(\\-\\d+)?$"
and so on
In many programming languages, the backslash \
can be misinterpreted in regular expressions. To use it properly, you must double it as \\
. For example, use \d +
.
Additionally, use * for options that may or may not occur, and use + for mandatory occurrences.
Lastly, remember that ^
should only be used at the beginning of a "sentence". It can also be used within a group as a negation like [^ az], but be cautious as it accepts "anything that is not ...".
Test your regular expression here:
Robular
This tool will show you exactly what matches your expression.
You can also organize match groups like this:
"^(\d+([\/\-]\d+)+)$"
This pattern means:
- It must begin with at least one digit
- It should contain at least one occurrence of digit, /, or - followed by another digit
- This structure can repeat indefinitely, but each repetition must include / or - followed by a digit