How to compute a regex match against an object's parameter by name?  
Author Message
BogusException





PostPosted: 2006-8-16 9:29:00 Top

java-programmer, How to compute a regex match against an object's parameter by name? Consider that you want to do a regex match on the contents of an
attribute of that object. Say, for example, the contents of:

SomeObjectInstance.x

All is well unless you don't know ahead of time that "x" is the
attribute that the user will search against. Suppose that the object
can have any attribute name, and even all those possibilities aren't
known at compile time. The user can/will select this attribute, as well
as the regex pattern.

So the trick is how to apply the match against "x" in this case with
only "x" as a string passed to the method?

// this doesn't work!
public boolean isRegexMatch(String p, String m, SomeObject o, String
attribute){
Pattern pat = Pattern.compile(p);
Matcher mat = pat.matcher(o."attribute"); <--- doesn't work, obviously
return (mat.find());
}

The above would be nice, but as expected-it doesn't work.

So the question is, how to make the method functional without knowing
what it will be passed for "attribute"?

TIA!

BogusException

 
ram





PostPosted: 2006-8-16 10:13:00 Top

java-programmer >> How to compute a regex match against an object's parameter by name? "BogusException" <email***@***.com> writes:
>o."attribute"

public class Main
{ public final java.lang.String attribute = "alpha";
public static void main( java.lang.String[] args )
throws
java.lang.NoSuchFieldException,
java.lang.IllegalAccessException
{ final Main main = new Main();
System.out.println
( main.getClass().getField( "attribute" ).get( main )); }}

alpha

 
Jean-Francois Briere





PostPosted: 2006-8-16 13:11:00 Top

java-programmer >> How to compute a regex match against an object's parameter by name? public boolean isRegexMatch(String pattern, Object obj, String attName)
throws Exception {
String attValue =
(String)obj.getClass().getField(attName).get(obj);
Matcher matcher = Pattern.compile(pattern).matcher(attValue);

return matcher.find();
}

 
 
BogusException





PostPosted: 2006-8-16 21:45:00 Top

java-programmer >> How to compute a regex match against an object's parameter by name? Jean-Francois and Stefan,

Thank you very much for your posts! I'm continually surprised at how
much this language can do, and how little it can't! I appreciate your
contribution to my work, and this group, very much.

:)

BogusException