.NET Simple Regular Expression

Posted on 2006-11-14
Last Modified: 2012-05-05
I need a regular expression which will match the following patter

somerandomtext HKS 1234 possiblysomemorerandomtext

From the above string, I would like to get back "HKS 1234". The number 1234 could be any number. There might not always be a space between HKS and the number. Thanks!
Question by:betocarranza
  • 2
LVL 62

Accepted Solution

Fernando Soto earned 210 total points
ID: 17938912
Hi betocarranza;

This should do it.

Imports System.Text.RegularExpressions

        Dim TestString As String = "somerandomtext HKS 1234 possiblysomemorerandomtext"

        Dim HKS As String = String.Empty
        Dim m As Match = Regex.Match(TestString, ".*?\s(HKS\s*\d+)\s+.*")
        If m.Success Then
            HKS = m.Groups(1).Value
        End If


Author Comment

ID: 17939007
Hi Fernando,

That works if the input is

blahblah HKS 1234 blahblah

but not if it is

blahblah HKS 1234


HKS 1234 blahblah rir


HKS 1234

Any ideas?

Author Comment

ID: 17939027
I got it -

      Dim m As Match = Regex.Match(TestString, "(HKS\s*\d+)")

Thanks for your help :)

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now