Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching '_' and '-' in java regexes

Tags:

java

regex

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 characters - 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?

like image 313
Pablo Fernandez Avatar asked Jan 23 '10 00:01

Pablo Fernandez


People also ask

How do you match a character in Java?

If you have a character you'll first need to convert it to a string before you can use a regular expression: if (Character. toString(symbol). matches("[A-Z?]")) { // ... }

How do I match a specific character in regex?

Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.

What does \\ mean in Java?

Notice that the regular expression String contains two backslashes after each other, and then a . . The reason is, that first the Java compiler interprets the two \\ characters as an escaped Java String character. After the Java compiler is done, only one \ is left, as \\ means the character \ .

What is regex pattern in Java?

Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java.


1 Answers

- 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-]+|~$
like image 131
cletus Avatar answered Oct 27 '22 19:10

cletus