RegEx for a JSON string - regex

Im storing a person object as JSON in my SQLite database. The table will have few 1000 records of person objects. What i need is to query person based on the "name" attribute.
After investigation i figured out using GLOB method of SQLite to perform a RegEx kind of search in the JSON elements.
My Sample JSON is something like this.
{"name":"john","age":"22","father-name":"jackson"}
Now i want a RegEx matcher to get me all the records that matches a part of the SubString provided with the name attribute in JSON. And it should be case insensitive too.
For Ex: "ohn" search should fetch me john's record.

While you can store JSON and search against it using regexes (which are rather limited in SQLite), it does not mean you should.
Instead, you should really consider splitting your JSON into fields and storing them in normal SQLite table. Doing so will not only allow you to perform easier searches without need to painfully parse data every single time, search will be much faster too (if you add necessary indexes).

If you do want to go down the regex route the following will extract the record:
/\{"name":"\w*ohn\w*[^\}]+\}/i
This will match each of these:
{"name":"john","age":"22","father-name":"jackson"}
{"name":"john","age":"22","father-name":"johnson"}
{"name":"johnny","age":"22","father-name":"smith"}
but not:
{"name":"fred","age":"22","father-name":"hall"},
{"name":"mike","age":"22","father-name":"johnson"}
{"name":"bob","age":"22","father-name":"todd"}

Related

How to order django query set filtered using '__icontains' such that the exactly matched result comes first

I am writing a simple app in django that searches for records in database.
Users inputs a name in the search field and that query is used to filter records using a particular field like -
Result = Users.objects.filter(name__icontains=query_from_searchbox)
E.g. -
Database consists of names- Shiv, Shivam, Shivendra, Kashiva, Varun... etc.
A search query 'shiv' returns records in following order-
Kahiva, Shivam, Shiv and Shivendra
Ordered by primary key.
My question is how can i achieve the order -
Shiv, Shivam, Shivendra and Kashiva.
I mean the most relevant first then lesser relevant result.
It's not possible to do that with standard Django as that type of thing is outside the scope & specific to a search app.
When you're interacting with the ORM consider what you're actually doing with the database - it's all just SQL queries.
If you wanted to rearrange the results you'd have to manipulate the queryset, check exact matches, then use regular expressions to check for partial matches.
Search isn't really the kind of thing that is best suited to the ORM however, so you may which to consider looking at specific search applications. They will usually maintain an index, which avoids database hits and may also offer a percentage match ordering like you're looking for.
A good place to start may be with Haystack

How to capture dynamic value in regular expression which depend on another dynamic value

I am new to Jmeter and I am looking for a way of capturing bolded "id" ("id":"56fa091ae4b081fb934b083a") which is having a dynamic data based on the "parentEventId" which is also a dynamic value in metadata section (I already have the "parentEventId" value taken from previous thread group). There can be multiple records with different "parentEventId"s and I need to get the mentioned "id" data of a selected "parentEventId".
This might have a simple answer but I couldn't find expression to use in If Controller and I have tried several RegEx extractors none of them worked and I couldn't find any positive answer for my question in web as well.
So any help on this would be highly appreciated, Thank you in advance
Below is my Body Data;
[{"id":"56fa091ae4b081fb934b083a","eventCalendar":{"id":"53757a6c156695a10bcefd57","type":"PERSON","displayName":"Test Name"},"organizerEvent":true,"start":1459486061505,"end":1459489661505,"allDay":false,"title":"test title","description":"test description","location":"test location","className":"cs-12","type":"CALENDAR","visibleToPublic":false,"metadata":{"parentEventId":"56fa091ae4b081fb934b0839"},
If your parentEventId and id are equal - why just not to use parentEventId? If not - update your question with full response and show relationships.
By the way, it's better to us JSON Path Extractor available via JMeter Plugins project for working with JSON Data, it's much more handy than regular expressions. For instance, you can extract parentEventId with something like:
$..parentEventId[0]
References:
JSON Path - XPath for JSON
Using the XPath Extractor in JMeter (scroll down to Parsing JSON)
If i understand correctly you are supplied with a dynamic parentEventId information and you would like to extract the child id from JSON data accordingly. If this is the case the following might be your solution
var parentEventId = "56fa091ae4b081fb934b0839",
data = '[{"id":"56fa091ae4b081fb934b083a","eventCalendar":{"id":"53757a6c156695a10bcefd57","type":"PERSON","displayName":"Test Name"},"organizerEvent":true,"start":1459486061505,"end":1459489661505,"allDay":false,"title":"test title","description":"test 56fa091ae4b081fb934b083adescription","location":"test location","className":"cs-12","type":"CALENDAR","visibleToPublic":false,"metadata":{"parentEventId":"56fa091ae4b081fb934b0839"},',
rex = new RegExp('\\[{"id":"([a-f0-9]{24})"(?=.*{"parentEventId":"' + parentEventId + '"})',"g"),
res = rex.exec(data);
console.log(res[1]); // 56fa091ae4b081fb934b083a

Query for field existence in CloudSearch

Suppose I have an optional field called 'xyz' in the list of documents I've indexed in CloudSearch.
How do I query CloudSearch so that it returns only those documents that contain 'xyz'?
If I know up front, that it's a positive integer, I can probably do something like this to get the required list:
q=xyz:[0,}&q.parser=structured
But how do I do it if 'xyz' stores some other type like a string or a list of ints/strings etc.,?
BTW, I've used Solr before, and there, I could simply do q=xyz:* to achieve this. Does CloudSearch support such regular expressions?
You can query for non empty values in a field using * operator, in your case its going to be xyz:* This will only work if you are using Lucene parser for your query to CloudSearch.

How to query mongodb with a regex in the projection?

Does anyone know if there is a way to query MongoDB and have only certain fields returned by using a regex as part of the projection?
For example: Given a collection having arbitrary field names, how might I query the collection and only return field names matching the regex '^foo'.
Possibly something like this?
db.mycollection.find({},{$regex:"^foo"})
Thanks.
Brent.
I think you need to break down the process into two pieces, the first one is retrieving the fields names from MongoDB.
Then the second piece is that you can run the regex on the result, and from there you can query the DB with the right fields.

Scan a dynamodb based on a list

I have a String Set attribute i.e SS in a dynamodb table. I need to scan the database to check the value present in the any one list of the items.
Which comparison operator should I use for this scan?
example the db has items like this:
name
[email1, email2]
phone
I need to search for a items containing a particular email say email1 alone not giving the entire tuple.
It seems like you are looking for the CONTAINS operator of Scan operation. It basically is the equivalent of in in Python.
This said, if you need to perform this often, you probably should de-normalize your data to make it faster.
For example, you could build a second table like this:
hash_key: name
range_key: email
Of course, you would have to maintain this index yourself and query it manually.