Get some of the text that is between tags in a String

0

I have the following string:

Meu nome é <nome> Nickolas Carlos<nome>

I would like to know how I can do in VB.NET to get only what is between the <nome> tags which in this case would be: Nickolas Carlos

Is it possible to do this using VB.NET only?

    
asked by anonymous 10.03.2015 / 20:56

3 answers

0

An option using regex:

Imports System.Text.RegularExpressions
Module VBModule
    Sub Main()
        Dim str As String = "Meu nome é <nome> Nickolas Carlos<nome>"
        Dim pad As New Regex("(?<=<nome> ).+(?=<nome>)")
        Dim nome As String = pad.Match(str).value
        Console.WriteLine(nome)
    End Sub
End Module

Just be careful with the spaces. In this case, it is considering a space after <nome> and none after the name.

    
31.03.2015 / 01:51
1

One possibility would be:

Sub Main()
    Dim minhaString = "Meu nome é <nome>Nicolas Carlos da Silva<nome>"
    Dim nome = minhaString.Substring(minhaString.IndexOf(">") + 1, minhaString.LastIndexOf("<") - minhaString.IndexOf(">") - 1)
    Debug.Print(nome)
End Sub
    
10.03.2015 / 22:05
0

I can do this in C # ... but I think you will not have problems converting to VB.Net:

string texto = "xpto <nome> abc </nome>";
var match = Regex.Match(texto, @"\<nome\>\s*(.+?)\s*?\<\/nome\>");
var nome = match.Success ? match.Groups[1].Value : null;

The return will be abc in the above example.

The regex is efficient because I use the *? and +? operators that try to match first the smaller alternatives rather than the larger ones first (which would be the case of * and +     

31.03.2015 / 02:27