Writing MYSQL REGEXP: greedy or negative confusion

I am looking for some regex help for a MYSQL query. I am new to expressions and totally confused.

My database cell looks something like this:

1314{{Here is some data}}1213{{More data here}}1112{{Data ahoy}} 

And I'm trying to write an expression that will try to match a dataset, but only the data in parentheses per year.

For example, say $ year = 1314 and $ term = "ahoy" .

With the REGEXP below:

 $year\{\{.* $term.*\}\} 

It returns a match - because it matches the final "}}" of the 1112 prefix dataset. I don't want this to be done, but despite reading this greedy / negative business, I can't get the syntax to work. What would be the best way to achieve what I need?

+4
source share
1 answer

Try:

 $year\{\{[^}]*$term[^}]*\}\} 

Where [^}]* matches something, not } , so it will no longer match when it hits the first occurrence }

.* is greedy, so it will match the last occurrence, so .*\}\} matches the last set }} , to make it inanimate, can you use ? like .*?\}\} but I prefer to use negation,

+4
source

All Articles