Check if string is valid using regular expresion
-
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
-
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
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.
- The initial
-
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.
- The initial