Page 1 of 1

Regex help

Posted: Fri May 29, 2009 12:50 am
by soysalyu
Hello,

I am trying to find a solution to this request by using regex but I couldn't write the right expression yet. Can you help with the expression?
  • Expression should match all text that contains "Exception"
  • Expression should not match if the text contains "LoadException"
  • Expression should not match if the text contains "ORA-20XXX" where X is a digit [0-9]
Here is the regex that i write:
(.(?<!(ORA-20[\d]{3})))*([\s]*\b([Ee]xception(?!([Ll]oad))(?!(ORA-20[\d]{3})))\b[\s]*)(.(?<!(ORA-20[\d]{3})))*

Thank you

Re: Regex help

Posted: Fri May 29, 2009 1:28 am
by prometheuzz
Try this:

Code: Select all

^(?s)(?=.*?Exception)(?!.*?LoadException)(?!.*?ORA-20\d{3})

Re: Regex help

Posted: Mon Jun 01, 2009 8:06 am
by soysalyu
prometheuzz: First of all, thank you for your help. This regex don't seem to match exception keyword. I tested it on http://www.fileformat.info/tool/regex.htm
For example, it should match following samples, but it doesn't.

* Exception occured
* An Exception occured
* A java.io.FileNotFoundException is thrown

Re: Regex help

Posted: Mon Jun 01, 2009 8:24 am
by prometheuzz
soysalyu wrote:prometheuzz: First of all, thank you for your help.
You're welcome.
soysalyu wrote:This regex don't seem to match exception keyword. I tested it on http://www.fileformat.info/tool/regex.htm
For example, it should match following samples, but it doesn't.

* Exception occured
* An Exception occured
* A java.io.FileNotFoundException is thrown
I'm sure it's correct, so you're probably using it incorrectly. I'm not going to try to see how that regex-tester tool works (I've seen it before and I find it a badly written tool). But since this is a PHP forum, I'll post a PHP snippet that shows it works properly:

Code: Select all

$tests = array(
    'Exception occured', 
    'An Exception occured', 
    'A java.io.FileNotFoundException is thrown',
    'and a LoadException here',
    'test ORA-20123 foo Exception',
    'bar ORA-2012X Exception baz' // note the 'X' is not a number, so it should match!
);
foreach($tests as $t) {
    if(preg_match('/^(?s)(?=.*?Exception)(?!.*?LoadException)(?!.*?ORA-20\d{3})/', $t)) {
        echo "OK   -> $t\n";
    } else {
        echo "fail -> $t\n";
    }
}
 
/* output:
    OK   -> Exception occured
    OK   -> An Exception occured
    OK   -> A java.io.FileNotFoundException is thrown
    fail -> and a LoadException here
    fail -> test ORA-20123 foo Exception
    OK   -> bar ORA-2012X Exception baz
*/