ASP.NET / VB / REGEX: Test for match instead of replace

Posted on 2012-09-18
Last Modified: 2012-10-08
The code below replaces matches inside a string.  Instead of replacing all matches inside the string, I want the entire string to be replaced if there is a match.

The string should become empty if there is no match and it should contain the full word if there is a match.

Please test this:
<%@ Page Language="VB" AutoEventWireup="false" %>
Dim str As String

str = "in is a state in America near Michigin"
str = RegularExpressions.Regex.Replace(str, "(?i)\bin\b", "Indiana")
' Should return "Indiana"

Response.Write("<hr />")

str = "Testing 123"
str = RegularExpressions.Regex.Replace(str, "(?i)\bin\b", "Indiana")
' Should not return anything


Open in new window

Question by:hankknight
    LVL 4

    Assisted Solution

    My VB/.net is a little rusty, but could you simply use RegularExpressions.Regex.IsMatch() to determine if there is a match and return your replacement value on TRUE or an empty string on FALSE?
    LVL 74

    Accepted Solution

    DustinKikuchi's example is viable (and easy to implement). For fun, I threw this together. It's definitely not as readable as the IsMatch approach, but it will do the same work:

    str = RegularExpressions.Regex.Replace(str, "\bin\b", Function(match As RegularExpressions.Match) If(match.Success, "Indiana", ""))

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    734 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

    19 Experts available now in Live!

    Get 1:1 Help Now