4

I'm looking for some help with a regular expression for a MYSQL query. I'm fairly new to expressions and have gotten myself thoroughly 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'll attempt to match the set of data, but only the data within the bracketed year.

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

With the below REGEXP:

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

It returns a match - because it's matching the final "}}" of the 1112 prefixed dataset. I don't want it to do this, but despite reading up on this greedy / negated business, I can't get the syntax to work. What would the best way to achieve what I'm after?

4

1 に答える 1

4

試す:

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

where[^}]*は a 以外のもの}に一致するため、最初に出現したときに一致を停止します}

.*貪欲なので、可能な限り最後の出現に.*\}\}一致するので、の最後のセットに一致し}}て非貪欲にしますが、?好きなものを使用できます.*?\}\}が、私は否定を使用することを好みます。

于 2012-12-21T15:08:07.627 に答える