How to find a random number in string

Posted on 2011-02-25
Last Modified: 2012-06-27

I can't seem to find a way for searching for  random number in a string. for example

"150 Opening BINARY mode data connection.
226 Transfer complete
ftp: 5590252 bytes sent in 9.45Seconds 591.31Kbytes/sec."

I need to find the number that is in bold above which could be a different number every time and be placed in a different position. It will however, always be after  "ftp: ". I then need to assign that number to a variable int.

Can anyone help on this please?

Question by:Michael Roodt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34983603
The line doesn't always start with "ftp:"?  i.e., it could sometimes be "Some other stuff ftp: 123345 etc etc"?
LVL 75

Accepted Solution

käµfm³d   👽 earned 250 total points
ID: 34983606
Try this:
Imports System.Text.RegularExpressions


Dim value As Integer
Dim m As Match = Regex.Match(sourceStr, "(?<=ftp: *)\d+")

If m.Success Then
    value = Convert.ToInt32(m.Value)
    value = -1
End If

Open in new window

LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
ID: 34983648
Almost same thing:

Imports System.Text.RegularExpressions
Module Module1

    Sub Main()
        Dim re As New Regex("ftp:\s+(\d+)\b")

        Dim test = "150 Opening BINARY mode data connection.226:    Transfer(complete) ftp: 5590252 bytes sent in 9.45Seconds 591.31Kbytes/sec."

        Dim m = re.Match(test)
        If m.Success Then
            Dim byteCount As Long
            If Int64.TryParse(m.Groups(1).Value, byteCount) Then
                Console.WriteLine("Found number: {0}", byteCount)
            End If
        End If

    End Sub

End Module

Open in new window

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34983698
The one caveat to tgerbert's pattern, and I don't think it will come to play here, is that if the number following "ftp: " were not followed by a space and had a string immediately following it (e.g.  "ftp: 5590252bytes"), then the match would fail due to the use of the word boundary at the end of the pattern. Again, I don't perceive that to be an issue here  = )

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question