Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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 …
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 …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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