1

The title is so confusing, I admit it - if you want to change it you are welcome.

I'm working on PHP and preg_match function.

The problem is this string (just an example)

ON DELETE SET NULL ON UPDATE CASCADE

ON DELETE CASCADE ON UPDATE CASCADE (another example..)

i need to find ON DELETE and ON UPDATE values, but they can't be there both.

So sometimes i have this:

ON DELETE something

and other times:

ON UPDATE something

and other times both.

So, this regex doesn't cover all possibilities:

/ON DELETE (.+) ON UPDATE (.+)/

If i put

/ON DELETE (.+)( ON UPDATE (.*))?/ -- to cover the case in which there isn't ON UPDATE

if ON UPDATE is present, first group results in "SET NULL ON UPDATE CASCADE".

How can i do that?

apelliciari
  • 8,241
  • 9
  • 57
  • 92

3 Answers3

2

Use lookaround if you want to avoid matching too much:

Negative lookahead :

/ON DELETE ((?:(?! ON UPDATE).)+)( ON UPDATE (.*))?/

To capture disregarding the order:

/(?:ON DELETE ((?:(?! ON UPDATE).)+)|ON UPDATE ((?:(?! ON DELETE).)+))/

But what exactly do you want? Remove both if they exist?

morja
  • 8,297
  • 2
  • 39
  • 59
  • ok, i updated my answer. if you want to match if both exist and also if only one of them exists, the second regex should do it. – morja Dec 14 '10 at 10:44
0

use Rubular to check your regexp

bw_üezi
  • 4,483
  • 4
  • 23
  • 41
0

psuedo code to match occurance of on delete and on update but not both

function valid(str)
{
 matchDelete=regex(str, 'ON DELETE'); //true for match
 matchUpdate=regex(str, 'ON UPDATE'); //true for match

 if(   (matchDelete || matchUpdate ) && !(matchDelete && matchUpdate)  )
 {
   valid match
 }
}
Matt
  • 3,778
  • 2
  • 28
  • 32