NULL
NULL

Reputation: 1589

get string after and before word

i need to get a set of values after certain 14 length of a string and before the last 5 stringss.

eg:

Theboyisgoingt9123holdi: so i need to get the value 9123 iamfullofmeats89holdi: i need to extract value 89

so the algorithm here is, i am trying to extract the values that comes after the 14th length of the string and just before the last 5 characters of the same string. its always whats between 14th and last 5 characters.

i am coding in vb.net. any ideas is great appreciated.

Upvotes: 2

Views: 2393

Answers (3)

steinar
steinar

Reputation: 9653

I too would go with a regex. This is how I would do it:

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim str As String = "Theboyisgoingt9123holdi"        
        Dim m As Match = Regex.Match(str, "^.{14}(.+).{5}$")
        If m.Success Then
            Console.WriteLine(m.Groups(1).Value)
        End If
    End Sub

End Module

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336258

Dim ResultString As String
ResultString = Regex.Match(SubjectString, "(?<=^.{14}).*(?=.{5}$)").Value

will give you the characters from the 15th until the 6th-to-last character in the string. It is assumed that there are no newlines in the string. If there are, and you want to treat them just like any other character, use the option RegexOptions.Singleline as an additional parameter to Regex.Match().

Explanation:

(?<=^.{14}): Match a position that is after the 14th character in the string.

.*: Match anything until...

(?=.{5}$): ...the position before the last 5 characters in the string.

Upvotes: 2

JaredPar
JaredPar

Reputation: 754943

This is a great place to use a regular expression

Function GetNumber(ByVal str As String) As String
  Dim match = Regex.Match("\d+", str)
  if math.Sucess then
    Return match.Value
  Else
    Return String.Empty
  End If
End Function

Upvotes: 0

Related Questions