[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-17
3
Medium Priority
?
717 Views
Last Modified: 2012-09-18
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

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38407299
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

0
 
LVL 16

Author Comment

by:hankknight
ID: 38407703
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?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38409445
Sorry, I thought this was classic ASP, not ASP.NET. Corrected:

<%@ 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 = System.Text.RegularExpressions.Regex.Replace(Xyz, "(?i)\b" & state.Key & "\b", 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

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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

873 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