Reputation: 327
I have a String to match with regex
String str = "(1a/2s-/-)"
The conditions I need to get:
regex I have tried so far
([A-Za-z0-9]/[A-Za-z0-9]+)
Does anyone could help me solve this?
Upvotes: 0
Views: 8903
Reputation: 7909
What you're missing is you need to escape the special characters that have meaning to a regular expression. Such as parenthesis, dashes and slashes.
\([a-zA-Z0-9\-\/]+\)
If you need to force that the string is nothing but this then make it looks like this:
^\([a-zA-Z0-9\-\/]+\)$
The ^
and the $
mean it must start and end with this respectively.
Broken down:
^
= Must start with
\(
= An opening parenthesis
[a-zA-Z0-9\-\/]+
= At least one or more alphanumeric chars, dashes or forward slashes
\)
= A closing parenthesis
$
= Must end with
Upvotes: 6
Reputation: 43023
You can use this regex:
^\([A-Za-z0-9\-\/]+\)$
You need to escape parentheses and put slash and dash into your character set. ^
and $
is optional if you want to match the string from the beginning to end.
Upvotes: 0