Making RegEx searches case-insensitive

Posted on 2006-04-03
Medium Priority
Last Modified: 2010-04-23
I’m using asp.net 1.1 with Visual Studio 2003 (vb.net).   I have a program that checks a url to make sure it’s valid and that it is a pdf file.  What I want to do is make it so the check is case-insensitive.  Below you can see how I used “[pP][dD][fF]” to make the search for “pdf” case-insensitive, but I don’t want to do it that way (I may want to later make other parts case-insensitive).

I see in the Help that there is an “IgnoreCase” option, but I don’t know how to utilize it.

The first line below makes the url criteria, then the conditional statement checks to see if the url is how I require it to be.  But I don’t see how to make it case-insensitive.  I don’t see how I would use “urlRegEx.Options.IgnoreCase”.  Can you help?  Thank you!

(pdfFileUrl is the url itself)

        urlRegEx = New Regex("^(http[s]?://).+[pP][dD][fF]$")
            If urlRegEx.IsMatch(pdfFileUrl) Then
                Trace.Warn("urlRegEx", "Url should be okay.")
                Trace.Warn("urlRegEx", "Url failed!")
            End If
Question by:psmithphil
  • 2
LVL 37

Expert Comment

by:Harisha M G
ID: 16363882
Hi, try this:

urlRegEx.IsMatch(pdfFileUrl, RegEx.Options.IgnoreCase)

LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 16364072
Hi psmithphil;

Just change this line in your code

urlRegEx = New Regex("^(http[s]?://).+[pP][dD][fF]$")

To this:

urlRegEx = New Regex("^(http[s]?://).+pdf$", RegexOptions.IgnoreCase)


Author Comment

ID: 16364229
Thank you both!

Fernando, what you showed me worked perfectly.  Now that I see it, I can see why it would work.  Mgh's was close but it wouldn't show up in Intellisense so it wasn't available.

This makes life a lot easier.  Thank you!
LVL 64

Expert Comment

by:Fernando Soto
ID: 16364271
Glad I was able to help. :=)

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

850 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