I must be doing my regex wrong.
In the console I do
db.triples.find({sub_uri: /.*pdf.*/ }); and get the desired result.
My Java class looks like this, (I have set input="pdf"):
    public static List<Triple> search(String input){
        DB db=null;
        try {
            db = Dao.getDB();
        }
        catch (UnknownHostException e1) {   e1.printStackTrace(); }
        catch (MongoException e1) {         e1.printStackTrace(); }
        String pattern = "/.*"+input+".*/";
System.out.println(input);      
                List<Triple> triples = new ArrayList<Triple>();
                DBCollection triplesColl = null;
                try {
                    triplesColl = db.getCollection("triples");      } catch (MongoException e) { e.printStackTrace();}
                {                   
                    Pattern match = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
                    BasicDBObject query = new BasicDBObject("sub_uri", match);
                    // finds all people with "name" matching /joh?n/i
                    DBCursor cursor = triplesColl.find(query);
                    if(cursor.hasNext()){
                    DBObject tripleAsBSON = cursor.next();
                        Triple t = new Triple();
                        t.setSubject(new Resource((String)tripleAsBSON.get("sub_uri")));
System.out.println(t.getSubject().getUri());                
                        triples.add(t);
                    }   
            }
        return triples;
    }
From the console I get 12 results as I should, from the Java code I get no results.
Java doesn't need/understand regex delimiters (/ around the regex). You need to remove them:
String pattern = ".*"+input+".*";
I'm also not sure if that regex is really what you want. At least you should anchor it:
String pattern = "^.*"+input+".*$";
and compile it using the Pattern.MULTILINE option. This avoids a severe performance penalty if a line doesn't contain your sub-regex input. You are aware that input is a regex, not a verbatim string, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With