I need to create a regular expression that allows a string to contain any number of:
- alphanumeric characters
- spaces
- (
- )
- &
- .
No other characters are permitted. I used RegexBuddy to construct the following regex, which works correctly when I test it within RegexBuddy:
\w* *\(*\)*&*\.*
Then I used RegexBuddy's "Use" feature to convert this into Java code, but it doesn't appear to work correctly using a simple test program:
public class RegexTest
{
public static void main(String[] args)
{
String test = "(AT) & (T)."; // Should be valid
System.out.println("Test string matches: "
+ test.matches("\\w* *\\(*\\)*&*\\.*")); // Outputs false
}
}
- I must admit that I have a bit of a blind spot when it comes to regular expressions. Can anyone explain why it doesn't work please?