【发布时间】:2015-07-02 01:13:16
【问题描述】:
我正在搜索将纯文本转换为这种格式的代码
我是说
例如,如果有纯文本 = v6.23b 12 Full
所以我希望它将这个文本转换成这种格式
格式
版本 6.23 Build 12
就是这样。格式中不应出现“full”字样
【问题讨论】:
标签: vb.net
我正在搜索将纯文本转换为这种格式的代码
我是说
例如,如果有纯文本 = v6.23b 12 Full
所以我希望它将这个文本转换成这种格式
格式
版本 6.23 Build 12
就是这样。格式中不应出现“full”字样
【问题讨论】:
标签: vb.net
怎么样
Dim oldText As String = "v6.23b 12 Full"
Dim newText As String = oldText.Replace("v", "version ").Replace("b", " Build").Replace("Full", String.Empty)
请注意,如果字符串中还有其他“v”或“b”,则会出现问题。
【讨论】:
SubString。如果您总是想删除“full”,请编码,如果您总是想删除“trial”,请编码。
强迫自己学习正则表达式,所以这似乎是一个很好的练习......
我使用这两个网站来解决这些问题:
http://www.regular-expressions.info/tutorial.html
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
这是我使用正则表达式的版本:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim version As String = "v6.23b 12 Full"
Debug.Print(version)
' Find a "v" at the beginning of a word boundary, with an optional space afterwards,
' that is followed by one or more digits with an optional dot after them. Those digits with an optional dot
' after them can repeat one or more times.
' Change that "v" to "version" with a space afterwards:
version = Regex.Replace(version, "\bv ?(?=[\d+\.?]+)", "version ")
Debug.Print(version)
' Find one or more or digits followed by an optional dot, that type of sequence can repeat.
' Find that type of sequence followed by an optional space and a "b" or "B" on a word boundary.
' Change the "b" to "Build" preceded by a space:
version = Regex.Replace(version, "(?<=[\d+\.?]+) ?b|B\b", " Build") ' Change "b" to "Build"
Debug.Print(version)
' Using a case-insensitive search, replace a whole word of "FULL" or "TRIAL" with a blank string:
version = Regex.Replace(version, "(?i)\b ?full|trial\b", "")
Debug.Print(version)
End Sub
End Class
【讨论】: