Matching '_' and '-' in java regexes
I had this regex in java that matched either an alphanumeric character or the tilde (~)
^([a-z0-9])+|~$
Now I have to add also the char开发者_开发百科acters -
and _
I've tried a few combinations, neither of which work, for example:
^([a-zA-Z0-9_-])+|~$
^([a-zA-Z0-9]|-|_)+|~$
Sample input strings that must match:
woZOQNVddd
00000
ncnW0mL14-
dEowBO_Eu7
7MyG4XqFz-
A8ft-y6hDu ~
Any clues / suggestion?
-
is a special character within square brackets. It indicates a range. If it's not at either end of the regex it needs to be escaped by putting a \
before it.
It's worth pointing out a shortcut: \w
is equivalent to [0-9a-zA-Z_]
so I think this is more readable:
^([\w-]+|~$
You need to escape the -
, like \-
, since it is a special character (the range operator). _
is ok.
So ^([a-z0-9_\-])+|~$
.
Edit: your last input String will not match because the regular expression you are using matches a string of alphanumeric characters (plus -
and _
) OR a tilde (because of the pipe). But not both. If you want to allow an optional tilde on the end, change to:
^([a-z0-9_\-])+(~?)$
If you put the -
first, it won't be interpreted as the range indicator.
^([-a-zA-Z0-9_])+|~$
This matches all of your examples except the last one using the following code:
String str = "A8ft-y6hDu ~";
System.out.println("Result: " + str.matches("^([-a-zA-Z0-9_])+|~$"));
That last example won't match because it doesn't fit your description. The regex will match any combination of alphanumerics, -, and _, OR a ~ character.
精彩评论