C# Regex Alpahnumeric String

I'd like to check whether a string contains the following Groups. All Strings MUST contain exactly 2&nbsp;<em>##</em>

I'd like to check whether a string contains the following Groups. All Strings MUST contain exactly 2 ##

String 1)

##Some_Foo_Text_1##

=> Expected result: String contains 1 Valid group

String 2)

##Some_Foo_Text_2#E+1##

=> Expected result: String contains 1 Valid group

String 3)

##Some_Foo_Text_3#e-1##

=> Expected result: String contains 1 Valid Group

String 4)

##Some_Foo_Text_4##E+1##

=>Expected result: String contains 1 valid Group (##Some_Foo_Text_4##) and 1 invalid Group (E+1##) the invalid Group is discarded

Now i came up with this regex

/([A-Za-z\+\-0-9])+/g

According to Regexr this does not match my string. Could you help me to take care of the ## at the beginning and End?

C/C++ vs. Rust: A developer’s perspective

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Originally published by Maourice Gonzalez at https://www.onmsft.com

C++ is an incredibly fast and efficient programming language. Its versatility knows no bounds and its maturity ensures support and reliability are second to none. Code developed in C++ is also extremely portable, all major operating systems support it. Many developers begin their coding journey with the language, and this is no coincidence. Being object-oriented means it does a very good job of teaching concepts like classes, inheritance, abstraction, encapsulation and polymorphism. Its concepts and syntax can be found in modern languages like C#, Java and Rust. It provides a great foundation that serves as a high speed on ramp to the more popular, easier to use and modern alternatives.

Now it’s not all rosy. C++ has a very steep learning curve and requires developers to apply best practices to the letter or risk ending up with unsafe and/or poor performing code. The small footprint of the standard library, while most times considered a benefit, also adds to the level of difficulty. This means successfully using C++ to create useful complex libraries and applications can be challenging. There is also very little offered in terms of memory management, developers must do this themselves. Novice programmers could end up with debugging nightmares as their lack of experience leads to memory corruption and other sticky situations. This last point has lead many companies to explore fast performing, safe and equally powerful alternatives to C++. For today’s Microsoft that means Rust.

The majority of vulnerabilities fixed and with a CVE [Common Vulnerabilities and Exposures] assigned are caused by developers inadvertently inserting memory corruption bugs into their C and C++ code - Gavin Thomas, Microsoft Security Response Center

Rust began as a personal project by a Mozilla employee named Graydon Hoare sometime in 2006. This ambitious project was in pre-release development for almost a decade, finally launching version 1.0 in May 2015. In what seems to be the blink of an eye it has stolen the hearts of hordes of developers going as far as being voted the most loved language four years straight since 2016 in the Stack Overflow Developer Survey.

The hard work has definitely paid off. The end result is very efficient language which is characteristically object oriented. The fact that it was designed to be syntactically similar to C++ makes it very easy to approach. But unlike the aforementioned it was also designed to be memory safe while also employing a form of memory management without the explicit use of garbage collection.

The ugly truth is software development is very much a trial and error endeavor. With that said Rust has gone above and beyond to help us debug our code. The compiler produces extremely intuitive and user friendly error messages along with great direct linking to relevant documentation to aid with troubleshooting. This means if the problem is not evident, most times the answer is a click away. I’ve found myself rarely having to fire up my browser to look for solutions outside of what the Rust compiler offers in terms of explanation and documentation.

Rust does not have a garbage collector but most times still allocates and release memory for you. It’s also designed to be memory safe, unlike C++ which very easily lets you get into trouble with dangling pointers and data races. In contrast Rust employs concepts which help you prevent and avoid such issues.

There are many other factors which have steered me away from C++ and onto Rust. But to be honest it has nothing to do with all the great stuff we’ve just explored. I came to Rust on a journey that began with WebAssembly. What started with me looking for a more efficient alternative to JavaScript for the web turned into figuring out just how powerful Rust turns out to be. From its seamless interop…

Automatically generate binding code between Rust, WebAssembly, and JavaScript APIs. Take advantage of libraries like web-sys that provide pre-packaged bindings for the entire web platform. – Rust website

To how fast and predictable its performance is. Everything in our lives evolves. Our smartphones, our cars, our home appliances, our own bodies. C++ while still incredibly powerful, fast and versatile can only take us so far. There is no harm in exploring alternatives, especially one as exceptional and with as much promise as Rust.

What do you guys think? Have you or would you give Rust a try? Let us know your thoughts in the comments section below.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

Why you should move from Node.js to Rust in 2019

Rust Vs. Haskell: Which Language is Best for API Design?

7 reasons why you should learn Rust programming language in 2019

An introduction to Web Development with Rust for Node.js Developers

What is Regular Expression in C#?

What is Regular Expression in C#?

In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex.

In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex.

The .Net Framework provides a regular expression engine that allows the pattern matching. Patterns may consist of any character literals, operators or constructors.

C# provides a class termed as Regex which can be found in System.Text.RegularExpression namespace. This class will perform two things:

  • Parsing the inputing text for the regular expression pattern.
  • Identify the regular expression pattern in the given text.

Example 1: Below example demonstrate the use of regex in Mobile Number Verification. Suppose you are making a form where you need to verify the user-entered mobile number then you can use regex.

// C# program to validate the Mobile
// Number using Regular Expressions
using System;
using System.Text.RegularExpressions;
  
class GFG {
      
    // Main Method
    static void Main(string[] args)
    {
        // Input strings to Match
        // valid mobile number
        string[] str = {"9925612824",
          "8238783138", "02812451830"};
          
        foreach(string s in str)
        {
            Console.WriteLine("{0} {1} a valid mobile number.", s, 
                        isValidMobileNumber(s) ? "is" : "is not");
        }
          
        Console.ReadKey();
    }
      
    // method containing the regex
    public static bool isValidMobileNumber(string inputMobileNumber)
    {
        string strRegex = @"(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9]
                {2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)";
          
        // Class Regex Repesents an
        // immutable regular expression.
        //   Format                Pattern
        // xxxxxxxxxx           ^[0 - 9]{ 10}$
        // +xx xx xxxxxxxx     ^\+[0 - 9]{ 2}\s +[0 - 9]{ 2}\s +[0 - 9]{ 8}$
        // xxx - xxxx - xxxx   ^[0 - 9]{ 3} -[0 - 9]{ 4}-[0 - 9]{ 4}$
        Regex re = new Regex(strRegex);
          
        // The IsMatch method is used to validate
        // a string or to ensure that a string
        // conforms to a particular pattern.
        if (re.IsMatch(inputMobileNumber)) 
            return (true);
        else
            return (false);
    }
} 

Output:

9925612824 is a valid mobile number.
8238783138 is a valid mobile number.
02812451830 is not a valid mobile number.

Example 2: Below example demonstrate the use of regex in Email ID Verification. Suppose you are making a form where you need to verify the user-entered email id then you can use regex.

// C# program to validate the Email
// ID using Regular Expressions
using System;
using System.Text.RegularExpressions;
   
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
   
        // Input strings for Match
        // valid E-mail address.
        string[] str = {"[email protected]", 
                  "parthmaniyargmail.com",
                            "@gmail.com"};
          
        foreach(string s in str)
        {
            Console.WriteLine("{0} {1} a valid E-mail address.", s,
                                isValidEmail(s) ? "is" : "is not");
        }
          
    }
      
    // Method to check the Email ID
    public static bool isValidEmail(string inputEmail)
    {
          
        // This Pattern is use to verify the email
        string strRegex = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
                            
        Regex re = new Regex(strRegex, RegexOptions.IgnoreCase);
          
        if (re.IsMatch(inputEmail))
            return (true);
        else
            return (false);
    }
} 

Output:

[email protected] is a valid E-mail address.
parthmaniyargmail.com is not a valid E-mail address.
@gmail.com is not a valid E-mail address.

Regex Syntax

There are many basic syntaxes like Quantifiers, Special Characters, Character Classes, Grouping & Alternatives are used for regular expressions.

Quantifiers:

Example 1:

/ C# program to demonstrate
// the * Quantifier
using System;
using System.Text.RegularExpressions;
  
class GFG {
      
    // Main Method
    public static void Main(string[] args)
    {
        // This will return any 
        // pattern b, ab, aab, ...
        Regex regex = new Regex(@"a*b"); 
          
        Match match = regex.Match("aaaabcd");
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: aaaab

Example 2:

// C# program to demonstrate
// the + Quantifier
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // this will return any pattern
        // like ab, aab, aaab, ....
        Regex regex = new Regex(@"a+b"); 
        Match match = regex.Match("aaabcd");
        if (match.Success)
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: aaab

Example 3:

// C# program to demonstrate
// the ? Quantifier
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
         // This return any pattern like b, ab
        Regex regex = new Regex(@"a?b");
          
        Match match = regex.Match("aaaabcd");
          
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: ab

Special Characters

Example 1:

// C# program to demonstrate
// the ^ Special Character
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // This will return if shyam exist
        // at the beginning of the line
        Regex regex = new Regex(@"^Shyam"); 
          
        Match match = regex.Match("Shyam is my pet name");
          
        if (match.Success)
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: Shyam

Example 2:

// C# program to demonstrate
// the $ Special Character
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // This return parth if it
        // exist at the end of the line
        Regex regex = new Regex(@"Parth$");
          
        Match match = regex.Match("My name is Parth");
          
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: Parth

Example 3:

// C# program to demonstrate
// the .(Dot) Special Character
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
         // This will return any word which 
         // contains only one letter between 
         // s and t
        Regex regex = new Regex(@"s..t");
          
        Match match = regex.Match("This is my seat");
          
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: seat

Example 4:

// C# program to demonstrate
// the \d Special Character
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
        // This will the return
        // the one digit character
        Regex regex = new Regex(@"\d");
          
        Match match = regex.Match("I am 19 years old");
          
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
          
    }
} 

Output:

Match Value: 1

Character Classes

Example 1:

// C# program to demonstrate
// the [] character class
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // This will return one charcter either
        // a or b or c which will cone first
        Regex regex = new Regex(@"[abc]"); 
          
        Match match = regex.Match("abcdef");
          
        if (match.Success)
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: a

Example 2:

// C# program to demonstrate
// the [a-z] character class
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // This will return any character 
        // between x and z inclusive
        Regex regex = new Regex(@"[x-z]"); 
          
        Match match = regex.Match("xmax");
          
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: x

Example 3:

// C# program to demonstrate
// the [^a-z] character class
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // This will return other x,
        // y and z character
        Regex regex = new Regex(@"[^x-z]"); 
          
        Match match = regex.Match("xmax");
          
        if (match.Success) 
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: m

Grouping and Alternatives

Example 1:

// C# program to demonstrate
// the grouping in regex
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // This will return pattern 
        // will cd, cdcd, cdcdcd, ...
        Regex regex = new Regex(@"(cd)+"); 
          
        Match match = regex.Match("cdcdde");
          
        if (match.Success)
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: cdcd

Example 2:

// C# program to demonstrate
// the grouping in regex
using System;
using System.Text.RegularExpressions;
   
class GFG {
      
    // Main Method
    static void Main()
    {
          
        // This will either d or e 
        // which ever comes first
        Regex regex = new Regex(@"d|e"); 
          
        Match match = regex.Match("edge");
          
        if (match.Success)
        {
            Console.WriteLine("Match Value: " + match.Value);
        }
    }
} 

Output:

Match Value: e