0

I have a few pages of JSON to search like this:

"company": "chocolate factory"
"position": "founder"
"company": "chocolate packagers"
"position": "packager"
"company": "chocolate shop"
"position": "clerk"

I want to match only if the first occurrence of "position" isn't followed by founder. The preceding example wouldn't match, but the following would match.

"company": "chocolate factory"
"position": "taster"
"company": "chocolate packagers"
"position": "founder"
"company": "chocolate shop"
"position": "clerk"

This looks like this will match the first occurrence of "position"

("position": ")?

I think this should match the first occurrence of "position" only if it's not followed by "founder"

("position": ")?(?![^"]*founder)

I've tried it on regexr.com but it seems not to work right.

What's the correct way to do this?

Edit: So to clarify, I can only use a search box on a web app. I'm guessing it's a javascript regex but I can't really tell what flags are used (which makes things difficult)... but I think it's an SQL database doing the regex search if that makes sense.

I want to match only the first occurrence of "position"

(?:"position": ")?

but only if it's not followed by "founder"

(?:"position": ")?(?![^"]*founder)

or

(?:"position": "(?![^"]*founder))?

This kind of seems to work on testing sites, but it doesn't seem to work in the web app. Since I don't understand the backend, I'm probably not going to figure this one out.

Edit: I figured out the problem. The is postgresql regex - the escapes are slightly different, and it looks like how it searches is a little different too. Basically, I don't think what I wanted will work.

user09274385
  • 55
  • 1
  • 9
  • 1
    You could use a regex, but I suspect the better way to do this would be to use an actual JSON parser. – Phylogenesis Jul 19 '17 at 15:45
  • Try something like [`^((?!"position")[\s\S])*"position"(?![:\s]*"founder")`](https://regex101.com/r/ro4YVS/1) (not sure if understand right) – bobble bubble Jul 19 '17 at 15:56
  • Little unclear what you're trying to do. Sounds like a classic [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). I agree with @Phylogenesis, it sounds like a JSON parser with a simple conditional is what you _actually_ need. Unless your example JSON is literally what you have, in which case you don't have JSON, you have a series of strings separated by colons. – CDahn Jul 23 '17 at 17:00

1 Answers1

0

This should match what you want.

^("(?!position).+?": ".+?"\n)+"position": "(?!founder)
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • That would match every instance of "position" not followed by "founder". I'm trying to match only the first instance of position, and only if it's not followed by founder. I really want to know if he is a founder now... I don't care if he was for a previous job. – user09274385 Jul 19 '17 at 22:10
  • I changed it to just catch first position in a string. I assume each JSON is a separate string otherwise you didn't include enough sample data. – NetMage Jul 19 '17 at 22:15