VB.Net 上的正则表达式只从左到右获取前几个数字 [英] Regex on VB.Net to get only first few numbers from left to right
问题描述
我对正则表达式很陌生,但就我一直在 Google 上闲逛而言,我唯一能做的就是通过正则表达式来解决我的问题.
I am pretty new on regular expressions, but as far as I've been wandering around Google, the only thing I could do to solve my problem is through regex.
我有一组字符串,它们具有以下模式:
I have a collection of strings which have patterns like these:
3 - 橙子,柠檬"
4 - 菠萝,橙子"
12 - 酸橙、草莓"
"3 - Orange, Lemon"
"4 - Pineapple, Orange"
"12 - Lime, Strawberry"
我当前的代码是:
Private Sub cmbKelas_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbKelas.SelectedIndexChanged
LblID.Text = cmbKelas.Text
End Sub
它为整个字符串提供输出..我想要的是 cmbKelas.Text 将剥离其值,因此每个字符串将变为:
Which gives output to entire string.. what I wanted is that cmbKelas.Text will have its value stripped so each string will changes into:
3"
4"
12"
"3"
"4"
"12"
只有前几个数字.评论、建议或指向正确链接/文章会有所帮助,因为到目前为止我无法找到易于理解的体面教程.
Only the first few numbers. A comment, suggestion, or point to the right links/articles would help because so far I couldn't manage to find a decent tutorials that easy to understand.
推荐答案
如果你想用正则表达式做到这一点,你可以使用这样的东西:
If you want to do this with Regular Expressions you could use something like this:
Dim ResultString As String
ResultString = Regex.Match(SubjectString, "^\d+").Value
其中 SubjectString 将是您要搜索的字符串.
Where SubjectString would be the string you are searching in.
说明(因为您不熟悉正则表达式):
Explanation (since you are new to regex):
^
表示字符串的开头(这就是为什么你不匹配不在开头的数字)\d
表示数字 0-9(这是 [0-9] 的缩写形式,它会做同样的事情)- 和
+
表示+
之前的字符将至少匹配一次.
- The
^
signifies the beginning of a string (that why you don't match number that are not at the beginning) \d
signifies a digit 0-9 (it's the short form of [0-9] which would do the same)- and
+
means that the character prior to+
will be matched at least once.
这样你就可以在字符串的开头匹配 1 到无限个数字.
That way you will match 1 to infinite digits at the beginning of the string.
这篇关于VB.Net 上的正则表达式只从左到右获取前几个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!