regex question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • OneSolution

    #1

    regex question

    I am trying to write a regular expression that will match all the exceptions
    generated in my application log file. I have done it as such:

    \bjava\.\w+\.\w +Exception:

    However, there are two exceptions that I know will occur and I don't care to
    catch them. So I need to modify the above regular expression to ignore the
    two exceptions that will get generated.

    I don't know how to do that. Your help will be much appreciated.

    Thanks,
    Z



  • Lasse Reichstein Nielsen

    #2
    Re: regex question

    "OneSolutio n" <onesolution@sb cglobal.net> writes:

    [RegExp to match exceptions in log fil][color=blue]
    > \bjava\.\w+\.\w +Exception:
    >
    > However, there are two exceptions that I know will occur and I don't care to
    > catch them. So I need to modify the above regular expression to ignore the
    > two exceptions that will get generated.[/color]

    This sounds like something that will be run on a server under your
    control, so you can know the version of Javascript being run. In that
    case, you can probably expect it to be a modern version of Javascript
    with the improved regular expressions of ECMAScript v3, including
    negative look-ahead (which is what you want).:

    \bjava\.\w+\.(? !SomeStupidExce ption|SomeOther Exception)\w+Ex ception:

    Are you sure you don't want to match, e.g.,
    java.util.regex .SomeRegexExcep tion
    (because in that case, you should change "\.\w+" to "(\.\w+)+") .

    If your version of Javascript doesn't support negative look-ahead,
    it gets much harder (so much harder that it's probably easier to
    not use just one RegExp, but do a two-step test that filters
    out the two unintersting exceptions in a second step).

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • OneSolution

      #3
      Re: regex question

      Thanks, I'll try it and let you know.


      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:ekk3ef1o.f sf@hotpop.com.. .[color=blue]
      > "OneSolutio n" <onesolution@sb cglobal.net> writes:
      >
      > [RegExp to match exceptions in log fil][color=green]
      >> \bjava\.\w+\.\w +Exception:
      >>
      >> However, there are two exceptions that I know will occur and I don't care
      >> to
      >> catch them. So I need to modify the above regular expression to ignore
      >> the
      >> two exceptions that will get generated.[/color]
      >
      > This sounds like something that will be run on a server under your
      > control, so you can know the version of Javascript being run. In that
      > case, you can probably expect it to be a modern version of Javascript
      > with the improved regular expressions of ECMAScript v3, including
      > negative look-ahead (which is what you want).:
      >
      > \bjava\.\w+\.(? !SomeStupidExce ption|SomeOther Exception)\w+Ex ception:
      >
      > Are you sure you don't want to match, e.g.,
      > java.util.regex .SomeRegexExcep tion
      > (because in that case, you should change "\.\w+" to "(\.\w+)+") .
      >
      > If your version of Javascript doesn't support negative look-ahead,
      > it gets much harder (so much harder that it's probably easier to
      > not use just one RegExp, but do a two-step test that filters
      > out the two unintersting exceptions in a second step).
      >
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > DHTML Death Colors:
      > <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      > 'Faith without judgement merely degrades the spirit divine.'[/color]


      Comment

      • Jc

        #4
        Re: regex question

        AFAIK, regular expressions aren't very good at defining what NOT to
        match when it comes to more than a single character. For example, [^a]
        says to match anything that's not an a, but there's not an easy way to
        tell it not to match an entire word, for example, [^MyException] says
        to match anything that's not in that list of characters, as opposed to
        the actual full word, as one might think.

        The easiest way to accomplish what you are trying to do might be to use
        multiple regular expressions and some logic, perhaps something like:

        if (bMatchesJavaEx ception && !bMatchesDontCa reExceptionA &&
        !bMatchesDontCa reExceptionB) {
        // add to list
        } //if

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: regex question

          "Jc" <google@weinric hs.com> writes:
          [color=blue]
          > AFAIK, regular expressions aren't very good at defining what NOT to
          > match when it comes to more than a single character.[/color]

          That's mostly correct. However, the version of regular expressions in
          ECMAScript v3 has negative look-ahead, and most modern browsers
          implement it. As with any new feature, it's not supported by older
          browsers.
          [color=blue]
          > For example, [^a] says to match anything that's not an a, but
          > there's not an easy way to tell it not to match an entire word, for
          > example, [^MyException] says to match anything that's not in that
          > list of characters, as opposed to the actual full word, as one might
          > think.[/color]

          As a side note, it's actually interesting to consider it means *not*
          to match something. :)

          It is conceptually simple to work with matches. "First I match
          something, and after that I match something else". You can't
          do the same with a non-match. What comes "after" a non-match?
          How long is it?

          A match is an existential claim: There exists some string that can be
          matched by this regular expression in some way. A non-match is a
          universal claim: For all strings starting at this point, none of them
          can be matched by the regular expression, no matter how you try.

          (And for the computer scientists, matching with a regular expression
          is easily "implemente d" using nondeterministi c automata :)


          Anyway, the most reasonable answer to these problems that I have heard
          so far is the solution used: negative lookahead. A zero-width "match"
          only if the regular expression can't match something at that point.
          [color=blue]
          > The easiest way to accomplish what you are trying to do might be to use
          > multiple regular expressions and some logic, perhaps something like:
          >
          > if (bMatchesJavaEx ception && !bMatchesDontCa reExceptionA &&
          > !bMatchesDontCa reExceptionB) {[/color]

          It's a good suggestion in any case.

          There are many things that are each easily checkable with a regular
          expression, but combining some of them in one expression requires an
          exponentially larger expression.

          It's not always simpler to do two things at the same time instead
          of sequentially :)

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          Working...