Hello all, I have a questions regarding using regular expressions. When do I use the brackets [] to enclose the regex?
Suppose I want ONLY n,y,N,Y then the regex is [nyNY] or [\w&&[nyNY]] works fine. Using the first regex in Sun Java Tutorial RegexTestHarness program I got:
Enter your regex: [nyNY] Enter input string to search: n I found the text "n" starting at index 0 and ending at index 1
Enter your regex: [nyNY] Enter input string to search: y I found the text "y" starting at index 0 and ending at index 1
Enter your regex: nyNY Enter input string to search: n No match found
Enter your regex: nyNY Enter input string to search: y No match found
Enter your regex: nyNY Enter input string to search: ny No match found
Enter your regex: nyNY Enter input string to search: nyNY I found the text "nyNY" starting at index 0 and ending at index 4
My question is once I remove the brackets why it only return a match if I type exactly as it (eg "nyNY")? What's the purpose of those []?