+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Tutorial: C# Regex

  1. #1
    NeedHelp Guest

    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.

  2. CODECALL Circuit advertisement

     
  3. #2
    bassio is offline Newbie
    Join Date
    May 2007
    Posts
    2
    Rep Power
    0
    thanks alot man, really nice.

  4. #3
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    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.

  5. #4
    bishisht is offline Newbie
    Join Date
    Apr 2010
    Location
    Kathmandu,Nepal
    Posts
    23
    Rep Power
    0

    Re: Tutorial: C# Regex

    Quote Originally Posted by v0id View Post
    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.
    yes for the people like me it would be wonderful if you place some tutorial on regular expressions

  6. #5
    Kai_it is offline Newbie
    Join Date
    Jun 2010
    Location
    http://i-techvn.net
    Posts
    7
    Rep Power
    0

    Re: Tutorial: C# Regex

    i'm rewrite follow your tut, but it show incorrect.



    are you sure write correct this code?

    sr i'm not not fluent English. I'm from Vietnam

  7. #6
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,875
    Rep Power
    24

    Re: Tutorial: C# Regex

    The author of this tutorial hasn't been active anymore since 2008.

    What's wrong with it? The screenshot tells me that it's working correct. I mean 55 IS a number...

  8. #7
    bishisht is offline Newbie
    Join Date
    Apr 2010
    Location
    Kathmandu,Nepal
    Posts
    23
    Rep Power
    0

    Re: Tutorial: C# Regex

    yeah the second code piece has a bug in it...it always show "This is a number" or a kinda that even when you enter a piece of text

  9. #8
    Kai_it is offline Newbie
    Join Date
    Jun 2010
    Location
    http://i-techvn.net
    Posts
    7
    Rep Power
    0

    Re: Tutorial: C# Regex

    Quote Originally Posted by oxano View Post
    The author of this tutorial hasn't been active anymore since 2008.

    What's wrong with it? The screenshot tells me that it's working correct. I mean 55 IS a number...
    but i'm type "xyz" then messengerbox show "this is a number. I don't know author of this tutorial hasn't been active

    Quote Originally Posted by bishisht View Post
    yeah the second code piece has a bug in it...it always show "This is a number" or a kinda that even when you enter a piece of text
    You've found the problem

    p/s: this is nice tutorial

  10. #9
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,875
    Rep Power
    24

    Re: Tutorial: C# Regex

    Oh didn't see the right part of your screenshot when i posted that...

    Yes in the second part of his code you must replace
    Code:
    string number = "64";
    by user input or something.

    The regex seems correct
    [0-9] -- means: a character between and including 0 and 9
    * -- means: 0,1 or more times

    So if the string you give has only characters between 0 and 9, it'll validate.

    for letters there is a difference in case and you can't do [a-Z] because it'll validate a bunch of extra stuff too. It uses the ascii table and you'll see that the range a-Z will be wrong: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

  11. #10
    Kai_it is offline Newbie
    Join Date
    Jun 2010
    Location
    http://i-techvn.net
    Posts
    7
    Rep Power
    0

    Re: Tutorial: C# Regex

    i'm tried "string number = "64";" as author's code, but it always show "This is a number"

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Need some help with Regex
    By Edvinas in forum C# Programming
    Replies: 3
    Last Post: 06-25-2010, 12:12 PM
  2. RegEx in C++
    By BlaineSch in forum C and C++
    Replies: 2
    Last Post: 11-14-2009, 12:02 AM
  3. Regex Expressions
    By John_L in forum Perl
    Replies: 13
    Last Post: 02-14-2009, 11:01 AM
  4. Tutorial: C# Regex
    By NeedHelp in forum Tutorials
    Replies: 0
    Last Post: 06-28-2006, 09:27 AM
  5. Tutorial: C# Regex
    By NeedHelp in forum C# Programming
    Replies: 0
    Last Post: 06-28-2006, 09:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

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