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. Visual Basic
  4. Check if string is valid using regular expresion

Check if string is valid using regular expresion

Scheduled Pinned Locked Moved Visual Basic
regexhelptutorial
3 Posts 3 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.
  • V Offline
    V Offline
    viettho
    wrote on last edited by
    #1

    this is my initial variable: Dim Operators() As String = {"=", "<=", ">=", "+", "-", "!="} Dim Codes() As String = {"UC", "CT", "LS", "PR", "P1", "P2", _ "P3", "P4", "P5", "P6", "P7", "P8", "P9", "PQ", _ "QT", "PC", "SR", "MV", "SC", "DC"} Could someone help me to how use regular expression to check an input of string is valid in the following order: Codes Operators Integer For example: UC = 3 Thanks. John

    M 1 Reply Last reply
    0
    • V viettho

      this is my initial variable: Dim Operators() As String = {"=", "<=", ">=", "+", "-", "!="} Dim Codes() As String = {"UC", "CT", "LS", "PR", "P1", "P2", _ "P3", "P4", "P5", "P6", "P7", "P8", "P9", "PQ", _ "QT", "PC", "SR", "MV", "SC", "DC"} Could someone help me to how use regular expression to check an input of string is valid in the following order: Codes Operators Integer For example: UC = 3 Thanks. John

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi John. How about this regular expression? ^(UC|CT|LS|PR|P1|P2|P3|P4|P5|P6|P7|P8|P9|PQ|QT|PC|SR|MV|SC|DC)\s*(=|<=|>=|\+|-|!=)\s*\d+$

      • The initial ^ makes the match at the beginning of the string (i.e. nothing can come before the code).
      • Next is a group of choices (your codes) surrounded by (parentheses) and seperated with vertical bars |.
      • The \s that follows means to match white space and the asterisk * says "zero or more times"... so the \s* allows for optional white space between the code and the operator.
      • Then the (=|<=|>=|\+|-|!=) group is another list of choices, seperated by |, surrounded by (...) like before. The + sign has special meaning in regular expressions, so it requires an escape backslash \ character in front of it to treat it as a literal + sign.
      • Next we have another \s*, again allowing for optional whitespace between the operator and the integer
      • We use \d+ to match one or more digits from 0 to 9... the \d means match a digit, and the + means "one or more times".
      • Finally, the $ dollar sign at the end matches the position at the end of the input string, meaning that no other characters may follow the digits.

      I hope this helps.

      A 1 Reply Last reply
      0
      • M Mike Ellison

        Hi John. How about this regular expression? ^(UC|CT|LS|PR|P1|P2|P3|P4|P5|P6|P7|P8|P9|PQ|QT|PC|SR|MV|SC|DC)\s*(=|<=|>=|\+|-|!=)\s*\d+$

        • The initial ^ makes the match at the beginning of the string (i.e. nothing can come before the code).
        • Next is a group of choices (your codes) surrounded by (parentheses) and seperated with vertical bars |.
        • The \s that follows means to match white space and the asterisk * says "zero or more times"... so the \s* allows for optional white space between the code and the operator.
        • Then the (=|<=|>=|\+|-|!=) group is another list of choices, seperated by |, surrounded by (...) like before. The + sign has special meaning in regular expressions, so it requires an escape backslash \ character in front of it to treat it as a literal + sign.
        • Next we have another \s*, again allowing for optional whitespace between the operator and the integer
        • We use \d+ to match one or more digits from 0 to 9... the \d means match a digit, and the + means "one or more times".
        • Finally, the $ dollar sign at the end matches the position at the end of the input string, meaning that no other characters may follow the digits.

        I hope this helps.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Thank you so much. This really helps. John

        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