Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB regex, I get a different answer from the Java API compared with the console

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.

like image 955
Ankur Avatar asked Oct 25 '11 06:10

Ankur


1 Answers

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?

like image 155
Tim Pietzcker Avatar answered Nov 15 '22 08:11

Tim Pietzcker