I don't like regular expressions...

Source: Internet
Author: User
Tags net regex expression engine

 

Introduction:

In fact, I like regular expressions: they are doing well. In fact, they are so good that almost all programmers must use it.

Unfortunately, whenever I need a regular expression, I will encounter the same problem: I almost forgot the damn syntax ..

. If I want to write one every day, I may easily remember them, but I rarely write a few words in a year ..

 

After getting tired of viewing and learning documents again and again, I decided to implement them through the string extension method ..

 

Background:

Regular Expressions are very powerful and concise when it is used to verify, extract, edit, replace, or delete a text in a given mode (such as an email address.

 

To use regular expressions correctly, you should:

  • A text for analysis.
  • A regular expression parsing engine.
  • A regular expression (the pattern used for search and analysis in text)

The syntax of a regular expression depends on the regular expression parsing engine you use. In the world of Microsoft, this Regular Expression Engine class is

System. Text. regularexpressions.

Its syntax is here: http://msdn.microsoft.com/en-us/library/az24scfc.aspx

If you want to introduce the regular expression syntax, read this article:

Http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial

 

Regular Expression problems:

They have their disadvantages: Concise and powerful syntaxes are very suitable for regular expression engines, but not for human reading.

When you are not familiar with their syntax, you can spend a whole day writing a correct expression, but it may take a longer time to verify it. It is another thing to make the regular expression meet your expectations.

 

Idea:

If you are familiar with SQL, you must know the like operator. Why not include this operator in C?

Why can't none of them handle most ofCommon OperationsWhat about the simple syntax that can be executed by the Regular Expression Engine?

 

A simplified syntax

... Means fewer operators. Here is my own list:

  • ?= Any operator
  • %= 0 or multiple characters
  • *= 0 or multiple characters, but no space (mainly words)
  • #= Any single number (0-9)

Here is an example of these simple Syntax:

  • A guid can be expressed :???????? -???? -???? -???? -????????????
  • An email address may be :*? @? *.? *
  • One date :##/##/####

 

The fanic of the regular expression has jumped to the table: Obviously, the last regular expression does not make sure it matches a suitable date, of course they are right (that expression can match 99/99/9999 ). when verification is involved, it cannot provide the same level of functionality at all.

 

Frequent operations

Which of the following operations do you need for the Regular Expression Engine?

  1. 1: Determine whether a text matches a given pattern:Like
  2. 2: Search for text based on a given mode:Search
  3. 3: extract the string information in the text:Extract

As another choice for using the regular expression engine, the three operators 'like', 'search' and 'delete' have been implemented through the string extension method.

 

Let's talk about their use first...

1. Determine whether a string is in the "like" mode.

If you know SQL, you can understand what I mean...

If a string matches the specified pattern, the like operator simply returns true.

All the examples below return true, which means the input strings match their pattern.

 

Example: A string is a guid.

var result = "TA0E02391-A0DF-4772-B39A-C11F7D63C495".Like("????????-????-????-????????????"); 

Example: String ending with guid.

var result = "This is a guid TA0E02391-A0DF-4772-B39A-C11F7D63C495".Like("%????????-????-????-????????????"); 

 

Example: A string starting with guid.

var result = "TA0E02391-A0DF-4772-B39A-C11F7D63C495 is a guid".Like("????????-????-????-????????????%"); 

Example: a string containing a guid

var result = "this string TA0E02391-A0DF-4772-B39A-C11F7D63C495 contains a guid".Like("%????????-????-????-????????????%"); 
 

2. Search for the given mode in a string

SearchThe extension method can find the first substring matching the given pattern in a string.

Example: Search for guids in strings

var result = "this string [TA0E02391-A0DF-4772-B39A-C11F7D63C495] contains a string matching".Search("[????????-????-????-????????????]")    Console.WriteLine(result); // output: [TA0E02391-A0DF-4772-B39A-C11F7D63C495]
 

3. Use the substring in the given mode 'tracting' string

Similar to like search, it returns a substring array that matches the pattern instead of the entire string.

Example: return the component of the guid in a string.

var result = "this string [TA0E02391-A0DF-4772-B39A-C11F7D63C495] contains a string matching".Extract("[????????-????-????-????????????]");   // result is an array containing each part of the pattern: {"TA0E02391", "A0DF", "4772", "B39A", "C11F7D63C495"}

Example: return the e-mail component in a string.

var result = "this string contains an email: toto@domain.com".Extract("*?@?*.?*")    // result is an array containing each part of the pattern: {"toto", "domain", "com"}
 

Here is the code:

This simple technique is: the three public extension methods rely on the getregex method to convert these concise expressions into a valid. net regular expression.

public static bool Like(this string item, string searchPattern){    return GetRegex("^" + searchPattern).IsMatch(item);} public static string Search(this string item, string searchPattern){    var match = GetRegex(searchPattern).Match(item);    if (match.Success)    {        return item.Substring(match.Index, match.Length);    }    return null;} public static List<string> Extract(this string item, string searchPattern){    var result = item.Search(searchPattern);    if (!string.IsNullOrWhiteSpace(result))    {        var splitted = searchPattern.Split(new[] { '?', '%', '*', '#' }, StringSplitOptions.RemoveEmptyEntries);        var temp = result;        var final = new List<string>();        splitted.ForEach(x =>            {                var pos = temp.IndexOf(x);                if (pos > 0) {                    final.Add(temp.Substring(0, pos));                    temp = temp.Substring(pos);                }                temp = temp.Substring(x.Length);            });        if (temp.Length > 0) final.Add(temp);        return final;    }    return null;}// private method which accepts the simplified pattern and transform it into a valid .net regex pattern:// it escapes standard regex syntax reserved characters // and transforms the simplified syntax into the native Regex onestatic Regex GetRegex(string searchPattern){    return new Regex(searchPattern            .Replace("\\", "\\\\")            .Replace(".", "\\.")            .Replace("{", "\\{")            .Replace("}", "\\}")            .Replace("[", "\\[")            .Replace("]", "\\]")            .Replace("+", "\\+")            .Replace("$", "\\$")            .Replace(" ", "\\s")            .Replace("#", "[0-9]")            .Replace("?", ".")            .Replace("*", "\\w*")            .Replace("%", ".*")            , RegexOptions.IgnoreCase);}

 

Conclusion:

The purpose of this article is not to replace RegEx, but to provide a simple way to solve the case where I need to use regular expressions in 80%.

This method makes simple tasks easier, and makes client code easier to write and understand unfamiliar regular expression syntax.

 

Conclusion:

1: This is a translation article. The original Article address:I don't like RegEx.... The idea of this article is interesting, so I will share it with you.

2: The comment in this article is also interesting. You can check it out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.