简体   繁体   中英

converting plain text into textbox format

I am searching for code that converts plain text to this format

I mean

If for example there is a plain text = v6.23b 12 Full

so i want that it converts this text to this format

Format

version 6.23 Build 12

that's it . the word " full " should not be seen in the format

How about

Dim oldText As String = "v6.23b 12 Full"
Dim newText As String = oldText.Replace("v", "version ").Replace("b", " Build").Replace("Full", String.Empty)

Note that you'll have issues if there are other "v"s or "b" in the string.

Forcing myself to learn Regular Expressions, so this seemed like a good exercise...

I used these two sites to figure these out:

http://www.regular-expressions.info/tutorial.html

https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

Here's my version using Regex:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM