using regex for special characters

143 views
Skip to first unread message

sam

unread,
Nov 30, 2016, 4:07:45 PM11/30/16
to mongodb-user
Need help on trying to find $regex query for finding special characters in element value.  

Example: I have Name column, the value comes out as "GAGN┬⌐". I would like to query all items that have any special characters in value. 

Query: 
db.collection.find({$and: 
  [{NAME: {$exists: true}}
  ,{NAME: {$ne:null}}
  ,{NAME: {$ne:""}}
  ,{NAME: {$not: /[0-9a-zA-Z-*_#,.\/&();@`'"]/ }} ]}
);


This query not identifying the above example item. where I'm missing. 

any help?

Stephen Steneker

unread,
Dec 1, 2016, 8:04:39 AM12/1/16
to mongodb-user
On Thursday, 1 December 2016 08:07:45 UTC+11, sam wrote:
Need help on trying to find $regex query for finding special characters in element value.  

Example: I have Name column, the value comes out as "GAGN┬⌐". I would like to query all items that have any special characters in value. 

Query: 
db.collection.find({$and: 
  [{NAME: {$exists: true}}
  ,{NAME: {$ne:null}}
  ,{NAME: {$ne:""}}
  ,{NAME: {$not: /[0-9a-zA-Z-*_#,.\/&();@`'"]/ }} ]}
);

Hi Sam,

What do you define as "special characters"?

Regex matching uses standard PCRE (Perl Compatible Regular Expressions), so there are a number of meta characters and character classes that could greatly simplify your regexes. For this particular case the POSIX character classes should be particularly useful.

If you're just aiming to find field values with characters outside the normal printable ascii values, you can reduce this query to something as concise as:

db.collection.find({NAME: /[^[:print:]]/})

The [] indicates a character class, the first ^ negates the match (i.e. find any character not in the following list), and [:print:] is the POSIX character class for all printable characters including space. 

I would also suggest using an online regular expression tool to more easily debug your regular expressions against test data. For example: https://regex101.com.

Regards,
Stephen
Reply all
Reply to author
Forward
0 new messages