+ Reply to Thread
Results 1 to 3 of 3

Thread: Tutorial: C# Regex

  1. #1
    Programming God NeedHelp is on a distinguished road
    Join Date
    May 2006
    Posts
    526

    Tutorial: C# Regex

    I figured I would write this little tutorial about C# and Regex since it is still fresh in my mind.

    The code below shows how to remove all spaces from a string:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace regexconsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create a string and display it to the console
                string display = "I have     several   spaces in me";
                Console.WriteLine(display);
    
                // Replaces all spaces with 1 space
                display = System.Text.RegularExpressions.Regex.Replace(display," +", " ");
                Console.WriteLine(display);
    
                // Make it pause
                Console.ReadLine();
    
            }
        }
    }

    Here is how to determine if a string is a number or not
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace regexconsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create an integer and display it
                string number = "64";
                Console.WriteLine(number);
    
                // Determine if it is a positive number
                if (System.Text.RegularExpressions.Regex.IsMatch(number, ("[0-9]*")))
                {
                    Console.WriteLine("This is a number");
                }
                else
                {
                    Console.WriteLine("This is not a number!");
                }
    
    
    
                // Make it pause
                Console.ReadLine();
    
            }
        }
    }

    That is it for now. I will post more later.
    I Need Help

  2. #2
    Newbie bassio is an unknown quantity at this point
    Join Date
    May 2007
    Posts
    2
    thanks alot man, really nice.

  3. #3
    Retired v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light
    Join Date
    Apr 2007
    Posts
    2,978
    Blog Entries
    3
    It would be nice if you could tell something about regular expressions, and how they works, for those people there doesn't know what it actually is.
    I wouldn't call this a tutorial, it had been better with, "Examples" or something.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial: C# Hello World
    By Jordan in forum CSharp Tutorials
    Replies: 15
    Last Post: 10-16-2008, 10:44 PM
  2. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 03:05 PM
  3. regex (yuck)
    By John in forum PHP Forum
    Replies: 9
    Last Post: 08-09-2006, 12:35 AM
  4. JSP Tutorial
    By encoder in forum JavaScript and CSS
    Replies: 0
    Last Post: 05-26-2006, 05:31 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts