Regex help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
soysalyu
Forum Newbie
Posts: 2
Joined: Thu May 28, 2009 11:57 pm

Regex help

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex help

Post by prometheuzz »

Try this:

Code: Select all

^(?s)(?=.*?Exception)(?!.*?LoadException)(?!.*?ORA-20\d{3})
soysalyu
Forum Newbie
Posts: 2
Joined: Thu May 28, 2009 11:57 pm

Re: Regex help

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex help

Post 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
*/
Post Reply