Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-09
5
Medium Priority
?
179 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
[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
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
rlh68 earned 1000 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 1000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

721 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