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

Posted on 2012-09-18
Medium Priority
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

Assisted Solution

DustinKikuchi earned 800 total points
ID: 38411122
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 75

Accepted Solution

käµfm³d   👽 earned 1200 total points
ID: 38411973
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
User art_snob (http://www.experts-exchange.com/M_6114203.html) 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…
Suggested Courses

864 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