Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


.NET Simple Regular Expression

Posted on 2006-11-14
Medium Priority
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
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
LVL 64

Accepted Solution

Fernando Soto earned 630 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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