Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
BogusException - 16 Aug 2006 02:28 GMT
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
Stefan Ram - 16 Aug 2006 03:13 GMT
>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 - 16 Aug 2006 06:11 GMT
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 - 16 Aug 2006 14:45 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.