Q:

Can I use regular expressions to find a method on a class in java?

I know how to find a method in java using a fixed string,
someClass.getMethod("foobar", argTypes);

but is there a way to use a regular expression rather than a fixed string to find a method on a given class? 
An example of the usage might be if I wanted to find a method that was called either "foobar" or "fooBar". Using a regular expression like "foo[Bb]ar" would match either of these method names.

A:

You should apply your regexp on getDeclaredMethods() reflection method (or GetMethods() if you want only the public ones).
[Warning: both methods will throw a SecurityException if there is a security manager.]
You apply it on each name of each method returned by getDeclaredMethod() and only memorize in a Collection the compliant Methods.
Something like!
try
{
  final Pattern aMethodNamePattern = Pattern.compile("foo[Bb]ar");
  final List<Method> someMethods = aClass.getDeclaredMethods();
  final List<Method> someCompliantMethods = new ArrayList<Method>();
  for(final Method aMethod: someMethods)
  {
    final String aMethodName = aMethod.getName();
    final Matcher aMethodNameMatcher = aMethodNamePattern.getMatcher(aMethodName);
    if(aMethodNameMatcher.matches() == true)
    {
       someCompliantMethods.add(aMethod);
    }
}
catch(...) // catch all exceptions like SecurityException, IllegalAccessException, ...

