Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

ASP.NET / VB / REGEX: Case Insensitive and at beginning, middle or end

The code below only changes matches that have a space on either side and are uppercase.

For example, "ak" should be replaced with "Alaska" however it is not because the case does not match.  Also, "CA" should be replaced with "California" however it is not because it is at the end of the line.
<%@ Page Language="VB" AutoEventWireup="false" %>
<% 

Dim states As New Dictionary(of string, string)
states.add("AK", "Alaska")
states.add("AL", "Albama")
states.add("CA", "California")

Dim Xyz As String = "Lakes in ak are cold but it is always hot in AL and can be hot or cold in CA"

For Each state In states
 xyz = xyz.Replace(" " & state.Key & " ", " " & state.Value & " ")
Next

Response.Write(xyz)

' Should say:
' Lakes in Alaska are cold but it is always hot in Alabama and can be hot or cold in California

%>

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Try:

<%@ Page Language="VB" AutoEventWireup="false" %>
<% 

Dim states As New Dictionary(of string, string)
states.add("AK", "Alaska")
states.add("AL", "Albama")
states.add("CA", "California")

Dim Xyz As String = "Lakes in ak are cold but it is always hot in AL and can be hot or cold in CA"

For Each state In states

 With New RegExp
 
   .Pattern = "\b" & state.Key & "\b"
   .Global = True
   .CaseInsensitive = True
   
   xyz = .Replace(state.Value)
   
 End With
Next

Response.Write(xyz)

' Should say:
' Lakes in Alaska are cold but it is always hot in Alabama and can be hot or cold in California

%>

Open in new window

Avatar of hankknight

ASKER

Thanks, but that gives me an error:
Compiler Error Message: BC30002: Type 'RegExp' is not defined.

Have you tested it?  Does it work for you?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial