Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
B2B (Business Integration)
BizTalk
Book Excerpts
Build and Deploy
C#
C++
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Graphics
Internet Explorer 8.0
Interviews
iPhone
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
Network
NHibernate
Object Oriented Development
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Product News
Product Reviews
Project Management
Python
Q&A
Reporting Services
REST
RIA Services
Ruby
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



State of .NET


 


TOWER 48

Reader rating:
Click here to read 20 comments about this article.
Article source: CoDe (2003 - May/June)


Article Pages:  1  2 3 4 5 6 7 - Next >


Getting Started With Regular Expressions

In this article, Jim will introduce you to regular expressions, what they are, why you would want to use them, and finally, how you can begin putting them to work in Visual Studio .NET.

Regular expressions. The name doesn't conjure up any grandiose ideas about what they are all about. How could it with the word "regular" in the title? For those of you who struggled to learn how to use them, you're probably thinking they should be renamed irregular expressions. How could something that looks like this...

^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$ 

...be called regular??? (That pattern, by the way, will validate nearly 99% of all e-mail addresses, but more about that later.)

I've broken this article down into three sections.

Describe regular expressions

Analyze some common patterns

Implement common expressions in Visual Studio .NET.

At the end of the article I've listed resources including Web sites, books, and regular expression software.

What Are Regular Expressions?

A regular expression allows you to efficiently search for strings within other strings using a pattern matching expression. You've probably used a regular expression before and didn't even know it. For example, have you ever used *.txt to search for or filter a list of files? If so, congratulations! You're a regular expression user!

Regular expressions play a key role in all kinds of text-manipulation tasks. Common uses include searching (matching) and search-and-replace. You can also use common expressions to test for specific conditions in a string, text file, Web page, or XML stream. You could use regular expressions as the basis for a program that filters spam from incoming mail. In this type of situation, you might use a regular expression to determine whether the "From:" e-mail address is the e-mail address of a known spammer. As a matter of fact, many e-mail filtering programs use regular expressions for exactly this reason.

One of the drawbacks to using a regex is that they tend to be easier to write than they are to read. Here's an example of a very common regex pattern.

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

If this pattern looks overwhelming right now, don't worry, you'll know what it means by the time you finish reading this article.

You construct regex patterns using a series of different characters called metacharacters. Table 1 lists the most common metacharacters.

&

By: Jim Duffy

Jim Duffy is founder and president of TakeNote Technologies, an award-winning training, consulting, and software development company specializing in .NET software developer training and helping clients create business solutions with Microsoft enterprise technologies like Virtual Earth. Jim's expertise is with Visual Studio, Visual Basic, ASP.NET, SQL Server and Visual FoxPro-to-.NET conversions. He has a BS degree in Computer and Information Systems and over 25 years of programming and training experience. He is an energetic trainer, skilled developer, and has been published in leading developer-oriented publications.

Jim is a Microsoft Regional Director, a Microsoft MVP award recipient since 2003, an INETA speaker, and is an entertaining and popular speaker at regional user groups and international developer conferences. He is also a co-host of Computers 2K9, a call-in radio show on WRBZ (AM 850), 850 The Buzz, in Raleigh, NC.

You can find additional information about Jim, TakeNote Technologies, links to his blog, as well as a public training class schedule, on-site training information, consulting information, and software development services at www.takenote.com.

jduffy@takenote.com



Table 1: Metacharacter listing
MetacharacterMeaning
Character Matching
.Matches any single character except the newline character.
[ ]Matches any one of the enclosed characters. You can specify a range using a hyphen, such as [0-9].
x|yMatches either x or y.
Position Matching
^Matches beginning of string.
$Matches end of string.
Repetition Matching
?Matches 0 or 1 instances of the preceding character.
+Matches 1 or more instances of preceding character.
\Indicates that the next character should not be interpreted as a regular expression special character.
*Matches 0 or more instances of preceding character.
( )Groups a series of characters.
{n}Matches exactly n instances of preceding character.
{n,}Matches at least n instances of preceding character (where n is an integer).
{n,m}Matches at least n and at most m instances of preceding character (where n and m are integers).
Special Characters
\sMatches a single white space character, including space, tab, form feed, and line feed. (Same as [\f\n\r\t\v])
\wMatches any alphanumeric character, including the underscore (same as [A-Za-z0-9_]).
\dMatches a digit character (same as [0-9]).
\fMatches a form feed.
\nMatches a line feed.
\rMatches a carriage return.
\tMatches a tab.
\vMatches a vertical tab.
\bMatches a word boundary, such as a space.


Article Pages:  1  2 3 4 5 6 7 - Next Page: 'Character Matching' >>

Page 1: Intro
Page 2: Character Matching
Page 3: Repetition Matching
Page 4: Special Characters
Page 5: Basic Credit Card
Page 6: The RegEx Class
Page 7: MatchCollection

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.3 out of 5

91 people have rated this article.

      CODE TRAINING

 

TOWER 48