Results 1 to 6 of 6

Thread: [RESOLVED] Variable That Equals Anything

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    20

    Resolved [RESOLVED] Variable That Equals Anything

    I need to find and replace #-#-# with nothing in a richtextbox.
    How would I tell it that if there is any number then a - then any number then - then any number. To replace #-#-# with a space. My attempted code is below.

    Code:
    RichTextBox1.Text = Replace(RichTextBox1.Text, #-#-# , " ")

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable That Equals Anything

    You would use a regular expression for this. I'm far from an expert with regular expressions so I usually give them a miss but this seemed like a rather easy one so I figured I'd give it a go. This worked for me in my one simple test:
    vb.net Code:
    1. RichTextBox1.Text = Regex.Replace(RichTextBox1.Text,
    2.                                   "\d-\d-\d",
    3.                                   String.Empty)
    The "\d" in the pattern stands for any numeric digit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    20

    Post Re: Variable That Equals Anything

    Quote Originally Posted by jmcilhinney View Post
    You would use a regular expression for this. I'm far from an expert with regular expressions so I usually give them a miss but this seemed like a rather easy one so I figured I'd give it a go. This worked for me in my one simple test:
    vb.net Code:
    1. RichTextBox1.Text = Regex.Replace(RichTextBox1.Text,
    2.                                   "\d-\d-\d",
    3.                                   String.Empty)
    The "\d" in the pattern stands for any numeric digit.
    Only thing is the Regex? It says it's not declared. What is it?

    Edit: It also doesn't work because it takes your
    Code:
    Replace(RichTextBox1.Text, "\d-\d-\d",
    Literaly and if you type \d-\d-\d then it will empty the string, but numbers will not work.
    Last edited by ImSimplyLegit; Jul 3rd, 2012 at 01:07 AM.

  4. #4
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Variable That Equals Anything

    Quote Originally Posted by ImSimplyLegit View Post
    Only thing is the Regex? It says it's not declared. What is it?

    Edit: It also doesn't work because it takes your
    Code:
    Replace(RichTextBox1.Text, "\d-\d-\d",
    Literaly and if you type \d-\d-\d then it will empty the string, but numbers will not work.
    try to imports system.text.regularexpressions

    then you can now declare the regex.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable That Equals Anything

    Regex is a class and, just like any other class or type at all for that matter, you have to either qualify it with its namespace or else import that namespace. The MSDN Library has a topic for every type it contains and each topic tells you what assembly (DLL) that type is defined in and what namespace it's a member of.

    @marniel647, just to be precise, there is no declaration in that code. It's making use of the Regex class but not declaring the Regex class or a variable of the Regex type. This would be a declaration:
    Code:
    Dim r As Regex
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    20

    Smile Re: Variable That Equals Anything

    Quote Originally Posted by marniel647 View Post
    try to imports system.text.regularexpressions

    then you can now declare the regex.
    It Works! Thank you soooo so much. This code was needed to help organize a big list of classes for a college. ty

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width