Nick Criswell
Nick Criswell

Reputation: 1743

Regex First Instance Before a Known String but After Repeating String

I am working on a regex problem that I can't crack. I have a list of strings that looks something like this:

"Some Leading Words, "ind": "ind1", "primary":true, some more words, "ind": "ind2", "primary":false"

I would like to pull back the primary indicator. In this case, that would be ind1. This can be accomplished with:

\bind\b(.*?)"primary":true

Or at least this gets close...I still have the "ind" and the "primary:true" part, but I am actually doing this in Excel using the VBScript Regular Expressions 5.5 library so I can RIGHT, LEFT, MID and SEARCH my way around things I know I don't want to include.

The trouble that I have is when the primary indicator is second in the string.

"Some Leading Words, "ind": "ind1", "primary":false, some more words, "ind": "ind2", "primary":true

On this one, I end up with everything between the first ind and the "primary":true which is more than I want.

(FYI: I parsing JSON from the NPI API site and need to return each physicians primary taxonomy if that context helps out.)

Upvotes: 0

Views: 53

Answers (1)

Quixrick
Quixrick

Reputation: 3200

So, here's one idea. Include the comma before the "primary":true and then change the dot . to be a character class that allows anything except a comma.

\bind\b([^,]*?), "primary":true

That will give you what you want.

But, if you don't want the extra stuff along with your value, you can use something like this:

\bind\b": "([^,]*?)", "primary":true

Here is a demo

Upvotes: 1

Related Questions