Checking regex pattern validity

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

    Checking regex pattern validity

    Is there a function like IS_VALID_REGEX( ) to check whether a pattern is
    valid (i.e. it compiles)? I'm storing a list of regex patterns in a
    table. It would be nice to be able to add a CHECK constraint to ensure
    that all the regexes are valid.

    If there isn't any, can I suggest Postgres add one? Although I know this
    can probably be done in plpgsql using exception handling, or done in
    plperl or plruby.

    --
    dave


    ---------------------------(end of broadcast)---------------------------
    TIP 7: don't forget to increase your free space map settings

  • Tom Lane

    #2
    Re: Checking regex pattern validity

    David Garamond <lists@zara.6.i sreserved.com> writes:[color=blue]
    > Is there a function like IS_VALID_REGEX( ) to check whether a pattern is
    > valid (i.e. it compiles)? I'm storing a list of regex patterns in a
    > table. It would be nice to be able to add a CHECK constraint to ensure
    > that all the regexes are valid.[/color]

    ... CHECK (('' ~ pattern) IS NOT NULL) ...

    regards, tom lane

    ---------------------------(end of broadcast)---------------------------
    TIP 8: explain analyze is your friend

    Comment

    • David Garamond

      #3
      Re: Checking regex pattern validity

      Tom Lane wrote:[color=blue]
      > David Garamond <lists@zara.6.i sreserved.com> writes:
      >[color=green]
      >>Is there a function like IS_VALID_REGEX( ) to check whether a pattern is
      >>valid (i.e. it compiles)? I'm storing a list of regex patterns in a
      >>table. It would be nice to be able to add a CHECK constraint to ensure
      >>that all the regexes are valid.[/color]
      >
      > ... CHECK (('' ~ pattern) IS NOT NULL) ...[/color]

      Not exactly what I wanted, but close enough. Thanks!

      However, what if only want to accept invalid regex patterns? Or what if
      invalid pattern should be converted to NULL automatically? I'd still
      vote for a function...

      --
      dave

      ---------------------------(end of broadcast)---------------------------
      TIP 5: Have you checked our extensive FAQ?



      Comment

      Working...