Solved

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

Posted on 2008-10-09
5
175 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do i create a simple array in a public class? 6 37
Subtract dates in vb.net 6 35
Convert a string to date 4 40
VB.NET Parsing UDP Bytes 15 38
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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