Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to validate regular expression pattern?

How to validate regular expression pattern?

Scheduled Pinned Locked Moved C#
regexquestiontutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gokselm
    wrote on last edited by
    #1

    Hi, I want to ask a question about Regular expressions. I want to validate the regular expression pattern if it is written correctly or not? I do not want to test a string against to a regular expression pattern but the regular expression pattern itself. Is there a way? Kind Regards

    H 1 Reply Last reply
    0
    • G gokselm

      Hi, I want to ask a question about Regular expressions. I want to validate the regular expression pattern if it is written correctly or not? I do not want to test a string against to a regular expression pattern but the regular expression pattern itself. Is there a way? Kind Regards

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Just create a new instance of the RegEx. If the regex is invalid, an exception is thrown (this is documented in the class constructor information):

      try
      {
      Regex re = new Regex("/mypattern/");
      Console.WriteLine("The regular expression is valid.");
      }
      catch
      {
      Console.WriteLine("The regular expression is invalid!");
      }

      If you move the re declaration outside, you can use it if it's valid after the catch (but then the catch should return or something to keep from trying to use the invalid Regex object).

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      G 1 Reply Last reply
      0
      • H Heath Stewart

        Just create a new instance of the RegEx. If the regex is invalid, an exception is thrown (this is documented in the class constructor information):

        try
        {
        Regex re = new Regex("/mypattern/");
        Console.WriteLine("The regular expression is valid.");
        }
        catch
        {
        Console.WriteLine("The regular expression is invalid!");
        }

        If you move the re declaration outside, you can use it if it's valid after the catch (but then the catch should return or something to keep from trying to use the invalid Regex object).

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        G Offline
        G Offline
        gokselm
        wrote on last edited by
        #3

        Thanks for your help Heath.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups