[英]Regex to evaluate a string with commas only integers VB.NET
I have a string like this:我有一个这样的字符串:
Correct Strings正确的字符串
Dim value As String = "45, 2111,56, 1, 9, 99, 62,"
Invalid Strings:无效的字符串:
Dim value As String = "10.01, 12,, . , , "
I need to evaluate this string that has this format indefinitely, The regex evaluate the commas who are in series and are only integers.我需要无限期地评估这个具有这种格式的字符串,正则表达式评估串联且仅为整数的逗号。
This one should work:这个应该有效:
^(\d+, ?)*\d*$
It allows a list of comma-separated numbers, with possibly a space between the comma and the next number.它允许一个逗号分隔的数字列表,逗号和下一个数字之间可能有一个空格。 An empty list (an empty string) is allowed as well.
空列表(空字符串)也是允许的。
Try this one:试试这个:
Public Function test(ByVal test_strring$) As Boolean
Try
Dim pattern$ = "(?s)^(\d+\s*,\s*)+$"
' (?s)^(\d+\s*,\s*)+$
'
' Match the remainder of the regex with the options: dot matches newline (s) «(?s)»
' Assert position at the beginning of the string «^»
' Match the regular expression below and capture its match into backreference number 1 «(\d+\s*,\s*)+»
' Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
' Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «+»
' Match a single digit 0..9 «\d+»
' Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
' Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
' Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
' Match the character “,” literally «,»
' Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
' Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
' Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Return Regex.IsMatch(test_strring, pattern, RegexOptions.None)
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.