Solved

Is Regex the best way to do simple pattern matching in VB?

Posted on 2008-10-09
5
168 Views
Last Modified: 2010-04-23
Is Regex the best way to do simple pattern matching in VB.NET.

Here I'm check in the string has the word News in it.

            Dim objMatch As Match

            objMatch = Regex.Match("News", sSave.ToString)

            If (objMatch.Success) Then
                '     sSave = "Got News"
            End If
0
Comment
Question by:vaultworld
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
rlh68 earned 250 total points
ID: 22683482

I use IndexOf for simply searching.

    Dim str As String = "Some text to search for the word News in."
        If Str.IndexOf("News") > 0 Then
            ' has news, do something
        End If
0
 
LVL 1

Author Comment

by:vaultworld
ID: 22683553
Thanks,  it does have a weakness.

If the string is the same size that your looking for it doesn't work.

Example looks for News inside of News
0
 
LVL 7

Assisted Solution

by:Zippit
Zippit earned 250 total points
ID: 22683671
actually...it does.  In the previous example, change the following line:

If Str.IndexOf("News") > 0 Then

To:

If Str.IndexOf("News") >= 0 Then

The it will work, even when searching for "News" inside of "News" (IndexOf returns -1 if no match, 0 if match is found at first character).

HTH
0
 
LVL 7

Expert Comment

by:Zippit
ID: 22683677
IndexOf is the way to go if you are looking for a simple string within another string.  Don't get RegEx unless you are looking for more complex patterns (like multiple strings separated by any number of characters, patterns, etc).
0
 
LVL 1

Author Closing Comment

by:vaultworld
ID: 31504882
Thanks a ton, both of you
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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now