Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Primer

Status
Not open for further replies.

yu217171

Programmer
Aug 2, 2002
203
CA
Hi everyone,

Regular expressions seem like an interesting concept and I've been reading up on them lately. But most of the material I've come across uses the \regularexpression\ format. I've seen this format in Perl or Unix based grep utilities.

Does .NET use this format? It doesn't seem like it because .NET will accept a regex in the format without the backslashes. What are the names of the different formats, if there are indeed more than one regex format (syntax?)

Thanks in advance,

Keith
 
.net will not accept patterns without backslashes, unless those patterns are simple text.

For example, the pattern "those" matches simply the substring those.

The backslash is used to escape characters, and inform the engine that you mean something else, so for example,

\d means a digit, and not backslash d. For that, you'd use \\d, since the \ escapes itself. This gets interesting in C# and Java, because you have to escape \ in strings anyway...

I don't know if that really answers your question. But you are right - they are interesting and useful. I used to write a string function for everything until I discovered them.

The official primer is in MSDN



Mark [openup]
 
Hi guys,

Firstly, thanks for the responses. It seems that regexes originate from the Unix platform. I've been reading that a lot of the utilities; sed, awk, perl, delimit regexes using forward slashes.

ie: /[A-Z]+(abc|xyz)*/

So my first post was wrong to assume that regexes are delimited by backslashes. All of the time when creating regexes (in addition to other things), I will use verbatim strings in C#. Otherwise, it's too confusing with all the backslashes.

I've also come across a few different flavours of regexes. Is the JavaScript syntax different from the .NET syntax? And if so, how do they differ?

Below is a few links that may be useful to other newbies and even seasoned regex users.

Primer:

.NET RegEx Tester (also contains the JavaScript syntax)

Keith
 
Yes as far as I know, the JavaScript regex's are different from those in .net. I think it is a case of .net being a superset of what is available in JavaScript. One place where this may lead to trouble is that the Regex Validation control in asp.net accepts a pattern, but this pattern must be JS compliant, as it gets executed on the client.

Most of the time, for anything except the most complex regex's, there will be no problem. Best to experiment, though.

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top