• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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

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")

Response.Write(str)
%>

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
informaniacCommented:
You could use the startsWith and endsWith check for string.
0
 
Terry WoodsIT GuruCommented:
Perhaps this?

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

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now