ASP.NET / VB / REGEX: Replace "at" but not "cat" or "ate"

Posted on 2012-09-18
Last Modified: 2012-09-18
Using ASP.NET, VB and Regular Expressions, how can I replace "at" but only if it is the entire word?  I want "AT" to be replaced if is the entire word but I do not want the "at" inside of "cat" to be replaced.

<%@ Page Language="VB" AutoEventWireup="false" %>
Dim str As String = "At bat fat at cat at"
str = RegularExpressions.Regex.Replace(str, "at", "***")
' Should be "*** bat fat *** cat ***"

Open in new window

Question by:hankknight
    LVL 35

    Expert Comment

    LVL 35

    Accepted Solution

    Dim input As String = "replace word at but not on cat"
    Dim pattern As String = "\bat\b"
    Dim replace As String = ""
    Dim result As String = Regex.Replace(input, pattern, replace)

    Open in new window


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    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…
    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…

    728 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