Solved

.NET Simple Regular Expression

Posted on 2006-11-14
3
193 Views
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!
0
Comment
Question by:betocarranza
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
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


Fernando
0
 
LVL 2

Author Comment

by:betocarranza
ID: 17939007
Hi Fernando,

That works if the input is

blahblah HKS 1234 blahblah

but not if it is

blahblah HKS 1234

or

HKS 1234 blahblah rir

or

HKS 1234

Any ideas?
Thanks!
0
 
LVL 2

Author Comment

by:betocarranza
ID: 17939027
I got it -

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


Thanks for your help :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

20 Experts available now in Live!

Get 1:1 Help Now