Re Expression
Re Expression
Re Expression
Advertisements
Previous Page
Next Page
A regular expression is a pattern that could be matched against an input text.
The .Net framework provides a regular expression engine that allows such
matching. A pattern consists of one or more character literals, operators, or
constructs.
Character escapes
Character classes
Anchors
Grouping constructs
Quantifiers
Backreference constructs
Alternation constructs
Substitutions
Miscellaneous constructs
For the complete list of methods and properties, please read the Microsoft
documentation on C#.
Example 1
The following example matches words that start with 'S':
using System;
using System.Text.RegularExpressions;
namespace RegExApplication
{
class Program
{
private static void showMatch(string text, string expr)
{
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc)
{
Console.WriteLine(m);
}
}
static void Main(string[] args)
{
string str = "A Thousand Splendid Suns";
Console.WriteLine("Matching words that start with 'S': ");
showMatch(str, @"\bS\S*");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces following result:
Example 2
The following example matches words that start with 'm' and ends with 'e':
using System;
using System.Text.RegularExpressions;
namespace RegExApplication
{
class Program
{
private static void showMatch(string text, string expr)
{
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc)
{
Console.WriteLine(m);
}
}
static void Main(string[] args)
{
string str = "make maze and manage to measure it";
Console.WriteLine("Matching words start with 'm' and ends with 'e':");
showMatch(str, @"\bm\S*e\b");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces following result:
Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure
Example 3
This example replaces extra white space:
using System;
using System.Text.RegularExpressions;
namespace RegExApplication
{
class Program
{
static void Main(string[] args)
{
string input = "Hello
World
";
string pattern = "\\s+";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces following result:
Original String: Hello
World
Replacement String: Hello World
return valid;
}
c# regex
jlafay
Sophie Ker
3,85632250
4313
1 You're setting it to false after it matches. jlafay May 16 '11 at 13:03
A TIP: Rather than writing a-zA-Z you can use ?i to make your regex pattern case
insensitive and then just write a-zwhere ever required. NeverHopeless Nov 12 '12 at
13:02
3 Answers
activeoldestvotes
{
return Regex.IsMatch(str, @"^[a-zA-Z]+$");
}
This code has the following benefits:
at 13:03
Daniel Hilgarth
75.9k1078158
Because of the anchors ^ and $, ^[a-zA-Z]+$ will match a string if it is entirely composed
of letters (probably what the OT intends, but you should update the explanation).
Ekkehard.Horner May 16 '11 at 13:10
@Ekkehard: IMHO, my explanation states exactly that... Daniel Hilgarth May 16 '11 at
13:11
up Use
vote5do
wn vote Regex.IsMatch(@"^[a-zA-Z]+$");
edited May 16 '11 at
share|improve this answer
13:45
mathieu
15.2k12361
up Regex reg = new Regex("^[a-zA-Z]+$");
vote4do ^ start of the string
wn vote [] character set
Regular expressions are an efficient way to process text. The following regular
expression looks complicated to a beginner:
Collapse | Copy Code
^\w+$
The PERL developer would smile. All this regular expression does is return the exact
same word entered that the expression is compared to. The symbols look very difficult
to understand, and are.The ^ symbol refers to the start of the string. The $ refers to
the end of the string. The \w refers to the a whole word with the characters A-Z, a-z, 09 and underscore. The + is simply 0 or more repetitions. The regular expression would
match:
Collapse | Copy Code
test
testtest
test1
1test
Another useful static method is Match(), which returns a Match object with all matches
in the input string. This is useful when more than one match exists in the input
text. The following code results in more than one match:
Collapse | Copy Code
Console.WriteLine(g.Value);
}
The expression groups are entered in parentheses. The example above returns three
groups; the entire text as the first match, the first word, and the second
word. Expression groups are useful when text needs to broken down and grouped into
several pieces of related text for storage orfurther manipulation.
A Quick Example
In this example, we validate an email address using regular expressions. My regular
expressionworks:
Collapse | Copy Code
^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$
However, this isnt the only expression used to validate email addresses. There are at
least two other ways that I have come across. There are many more.
We write a small C# console application that takes some text as an input, and
determines if the text is an email address.
Collapse | Copy Code
using System.Text;
using System.Text.RegularExpressions;
string text = Console.ReadLine();
string reg = @"^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$";
if (Regex.IsMatch(text, reg))
{
Console.WriteLine("Email.");
}
else
{
Console.WriteLine("Not email.");
}
Try this with a few real and fake email addresses and see if it works. Let me know if
you find an error.
Documentation
Regular expressions are developed differently. The same task can be accomplished
using many different expressions. Expressions created by a developer may be
undecipherable by another.
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
This may be considered a long set of comments for a lot of development standards, but
the expression has been broken down into expression groups. A new developer has
very little difficulty in understanding the function and motivation behind writing the
expression. This practice should be consistently enforced to avoid headaches when
upgrading or debugging software.
by UltraPico (www.ultrapico.com) is a free Regular Expression Editor which you can use
to build and test your regular expressions.
Conslusion
Regular expressions are an efficient way to search, identify and validate large quantities
of text without having to write any comparisons. Although they may be complicated,
writing and documenting regular expressions allows the developer to concentrate on
more important parts of the implementation process. The use of several free and open
source regular expression tools makes understanding and building regular expressions
a worthwhile task.
To download this technical article in PDF format, go to the Coactum Solutions website
athttp://www.coactumsolutions.com/Articles.aspx.
C# Regex.Match
Regex.Match searches strings
based on a pattern. It isolates
part of a string based on the
pattern specified. It requires that
you use the text-processing
language for the pattern. It
proves to be useful and effective
in many C# programs.
String
Input and output required for examples
Input string:
/content/some-page.aspx
Input string:
/content/alternate-1.aspx
Input string:
/images/something.png
Required match: -
Example
We first see how you can match
the filename in a directory path
with Regex. This has more
constraints regarding the
acceptable characters than many
methods have. You can see the
char range in the second
parameter to Regex.Match.
Program that uses Regex.Match: C#
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// First we see the input string.
string input = "/content/alternate-1.aspx";
RegexOptions.IgnoreCase);
Output
alternate-1
ToLower
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// This is the input string.
string input = "/content/alternate-1.aspx";
}
}
Static Regex
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// The input string again.
string input = "/content/alternate-1.aspx";
Output
alternate-1
Numbers
int.Parseint.TryParse
Program that uses Match on numbers: C#
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// ... Input string.
string input = "Dot Net 100 Perls";
Output
100
Performance
Summary
The System.Text.RegularExpressions.Regex class can be used to search strings. These searches can range
in complexity from very simple to making full use of regular expressions. The following are two examples
of string searching by using the Regex class. For more information, see .NET Framework Regular
Expressions.
Example
The following code is a console application that performs a simple case-insensitive search of the strings in
an array. The static method Regex.IsMatch performs the search given the string to search and a string that
contains the search pattern. In this case, a third argument is used to indicate that case should be ignored.
For more information, see System.Text.RegularExpressions.RegexOptions.
C#
class TestRegularExpressions
{
static void Main()
{
string[] sentences =
{
"C# code",
"Chapter 2: Writing Code",
"Unicode",
"no match here"
};
string sPattern = "code";
foreach (string s in sentences)
{
System.Console.Write("{0,24}", s);
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", sPattern);
}
else
{
System.Console.WriteLine();
}
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
C# code (match for 'code' found)
Chapter 2: Writing Code (match for 'code' found)
Unicode (match for 'code' found)
no match here
*/
The following code is a console application that uses regular expressions to validate the format of each
string in an array. The validation requires that each string take the form of a telephone number in which
three groups of digits are separated by dashes, the first two groups contain three digits, and the third
group contains four digits. This is done by using the regular expression ^\\d{3}-\\d{3}-\\d{4}$. For
more information, see Regular Expression Language - Quick Reference.
C#
class TestRegularExpressionValidation
{
static void Main()
{
string[] numbers =
{
"123-555-0190",
"444-234-22450",
"690-555-0178",
"146-893-232",
"146-555-0122",
"4007-555-0111",
"407-555-0111",
"407-2-5555",
};
string sPattern = "^\\d{3}-\\d{3}-\\d{4}$";
foreach (string s in numbers)
{
System.Console.Write("{0,14}", s);
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))
{
System.Console.WriteLine(" - valid");
}
else
{
System.Console.WriteLine(" - invalid");
}
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
123-555-0190
444-234-22450
690-555-0178
146-893-232
146-555-0122
4007-555-0111
407-555-0111
407-2-5555
*/
valid
invalid
valid
invalid
valid
invalid
valid
invalid