[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

.NET Simple Regular Expression

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

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

It’s quite interesting for me as I worked with Excel using vb.net 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 …
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…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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