1

I am new to regular expression, I have stored JSON data in table. I want to fetch data based on userId, userId is a property of Users Object.

Example: I want fetch all the records if users contain 513

My table data sample:

 Table name:share

Id  name            sharedusers
1   xxxx            {"accessType":0,"permission":"1","specificUsers":
                    [{"users":"502,512,513","permission":"1"}]}

2   yyy             {"accessType":0,"permission":"1","specificUsers":
                    [{"users":"47,52,60","permission":"1"}]} 

3   zzzz            {"accessType":0,"permission":"1","specificUsers":
                    [{"users":"502,512,513","permission":"1"}]}  

I have tried this code but I am getting all the records, filter is not applied

SELECT * FROM share where sharedusers  REGEXP '"users":"[[:<:]]513[[:>:]]"';

Please give any sample.

0

2 Answers 2

4

You can use

REGEXP '"users":"[^"]*[[:<:]]513[[:>:]]'

Here, [^"]* (0+ characters other than ") is added and final " is removed to effectively match 513 as a whole word inside a pair of quotes that enclose the value.

Sign up to request clarification or add additional context in comments.

7 Comments

how to convert this ( '"users":"[^"]*[[:<:]]513[[:>:]]' )RegEx to String defining a regular expression (for example, '^foo' ). Regular expression literal (for example, /^foo/ ). Regular expression object (for example, new RegExp(/John/)).
if you need more details please look this link docs.strongloop.com/display/public/LB/…
You mean you need a JavaScript compatible regex? Use /"users":"[^"]*\b513\b/
@Thanks for your response. I tried in node.js but still I am getting error. can you give same(/"users":"[^"]*\b513\b/) in string defining format?
I tried in javascriipt but I am getting undefined. var patt = new RegExp(/"users":"[^"]*\b513\b/); var json= {"accessType":0,"permission":"1","specificUsers": [{" users":"502,512,513","permission":"1"}]}; var res = patt.json;
|
1

Upgrade to MySQL 5.7 and use the builtin functions for interrogating JSON columns.

4 Comments

Thanks for your response. I will try.
Though, those functions don't include regex support as far as I recall. Including mysql 8
@John - Use the Json functions instead of trying parse with a REGEXP.
@RickJames It's not a real solution, JSON_SEARCH and similar are by magnitudes less flexible than regular expressions. In my specific case I have hundreds of regex codes with complex content already existing, I can't switch. So I have to treat the json as text and search it that way which likely introduces a few problems due to escaping etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.