?
Solved

.NET Simple Regular Expression

Posted on 2006-11-14
3
Medium Priority
?
234 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
[X]
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
3 Comments
 
LVL 63

Accepted Solution

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


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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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