ASP.NET / VB / REGEX: Replace match if at beginning or end or string

Posted on 2012-09-18
Last Modified: 2012-10-08
I only want to replace "in" with "Indiana" if the matching "in" is at the very beginning or very end of a string.  If the match is found in the middle it should not be replaced.

<%@ Page Language="VB" AutoEventWireup="false" %>
Dim str As String = "in is a state in America near Michigin"

str = RegularExpressions.Regex.Replace(str, "(?i)\bin\b", "Indiana")


Open in new window

Question by:hankknight
    LVL 20

    Expert Comment

    You could use the startsWith and endsWith check for string.
    LVL 34

    Accepted Solution

    Perhaps this?

    str = RegularExpressions.Regex.Replace(str, "(?i)(^in\b|\bin$)", "Indiana")

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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.…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    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…

    760 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

    11 Experts available now in Live!

    Get 1:1 Help Now