Jump to content

Tutorial: C# Regex

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
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:


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

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
bassio

bassio

    Newbie

  • Members
  • Pip
  • 2 posts
thanks alot man, really nice.

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.

#4
bishisht

bishisht

    Newbie

  • Members
  • PipPip
  • 23 posts

v0id said:

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

#5
Kai_it

Kai_it

    Newbie

  • Members
  • Pip
  • 7 posts
i'm rewrite follow your tut, but it show incorrect.

Posted ImagePosted Image

are you sure write correct this code?

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

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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...

#7
bishisht

bishisht

    Newbie

  • Members
  • PipPip
  • 23 posts
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

#8
Kai_it

Kai_it

    Newbie

  • Members
  • Pip
  • 7 posts

oxano said:

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

bishisht said:

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

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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
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

#10
Kai_it

Kai_it

    Newbie

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

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Could you show us the code you're using? (in code tags, click the # button )

#12
Kai_it

Kai_it

    Newbie

  • Members
  • Pip
  • 7 posts
same author's code

string number = txtInput.Text;

            if (System.Text.RegularExpressions.Regex.IsMatch(number, ("[0-9]*")))

            {

                MessageBox.Show("This is a number");

            }

            else

            {

                MessageBox.Show("This is not a number");

            }