在VB.NET中获取字符之间的数字

时间:2019-02-24 14:30:49

标签: string vb.net char

这是我的第一个问题。 我有一个这样的字符串:

some text [low=123 medium=456 high=789]

我想读取所有数字,并在标签或类似的内容中键入它:

label1. text = low 
label2. text = medium 
label3. text = high

2 个答案:

答案 0 :(得分:1)

您可以为此使用正则表达式:

Dim RegexObj As New Regex("low=(?<low>\d+)\s+medium=(?<medium>\d+)\s+high=(?<high>\d+)")

label1.Text = RegexObj.Match(theString).Groups("low").Value 
label2.Text = RegexObj.Match(theString).Groups("medium").Value  
label3.Text = RegexObj.Match(theString).Groups("high").Value 

正则表达式详细信息

"low="            ' Match the characters “low=” literally
"(?<low>"         ' Match the regular expression below and capture its match into backreference with name “low”
   "\d"           ' Match a single digit 0..9
      "+"         ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")"  
"\s"              ' Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   "+"            ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"medium="         ' Match the characters “medium=” literally
"(?<medium>"      ' Match the regular expression below and capture its match into backreference with name “medium”
   "\d"           ' Match a single digit 0..9
      "+"         ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")"  
"\s"              ' Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   "+"            ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"high="           ' Match the characters “high=” literally
"(?<high>"        ' Match the regular expression below and capture its match into backreference with name “high”
   "\d"           ' Match a single digit 0..9
      "+"         ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")"  

答案 1 :(得分:0)

剪切文本的第一个和最后一个字符 用空格作为分隔符abd分割结果,然后用“ =”作为分隔符分割结果数组。 那你有你的结果。 如果顺序不是固定的,则必须检查第一个数组元素是否为中等,依此类推。