What should be the RegEx if I want to find the text between the phrases " IF (NEW.
" and " !
"?
Likewise I am thinking of the pattern 开发者_运维问答as $pattern = '/(?<= IF (NEW.)[^ !]+/';
I am matching it as
$input = $row4['ACTION_STATEMENT'];`/*BEGIN IF (NEW.name != OLD.name) THEN INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.name, newvalue = NEW.name, field = "name"; END IF; IF (NEW.alias != OLD.alias) THEN INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.alias, newvalue = NEW.alias, field = "alias"; END IF; END*/`
preg_match_all($pattern, $input, $captures);
What if I want $captures as an array, in which all values will be stored which will match with above pattern being in between of this pattern?
You can try:
if(preg_match('#IF \(NEW\.([^!]*)!#',$input,$matches)) {
echo "$matches[1]";
}
or
if(preg_match('#IF \(NEW\.(.*?)!#',$input,$matches)) {
echo "$matches[1]";
}
If you want to allow any amount of white space between IF
and (NEW
you can use the regex:
#IF\s*\(NEW\.(.*?)!#
If you want to allow atleast one white space between IF
and (NEW
you can use the regex:
#IF\s+\(NEW\.(.*?)!#
Try this:
preg_match("/IF \\(NEW\\.\\)(.*?)!/", $input, $captures);
Test:
$input = "foo bar IF (NEW.) capture this text! fu bah";
preg_match("/IF \\(NEW\\.\\)(.*?)!/", $input, $captures);
print_r($captures);
// ["IF (NEW.) capture this text!", " capture this text"]
You can use following pattern :
IF\s*?(NEW.[\s\S]*?!
Are you using perl?
精彩评论