Results 1 to 10 of 10

Thread: Removing inputs that are only alphabetic values

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2024
    Posts
    11

    Removing inputs that are only alphabetic values

    I have been looking for a way to filter out inputs that only contain alphabetic letters and whitespaces, I have seen a lot of people using the RegEx function. Though I am having trouble understanding the syntax of it, I read the page on microsoft's learn page about it. Yet I still am having a hard time trying to understand how I would lay out the function.

    In my situation I have a variable that will be an alpha-numeric string, but never should be fully alphabetic, so I am trying to keep values that may have a few letters but filter out inputs that are only letters (and whitespaces). So would I just be using something similar to:
    Code:
     If partwrite = True Then
           If Regex.IsMatch(InputVar, "^[A-Za-z]+$")
                partpos = partpos - 1 'subtracting 1 since Do loop adds 1 extra
                mydriver.Write($"{"Part_Number["}{partpos}{"].Paint_Color"}", paintcolor)
                mydriver.Write($"{"Part_Number["}{partpos}{"].Oven_Cycle"}", OVEN_N)
                mydriver.Write($"{"Part_Number["}{partpos}{"].IR_Zone1_Percentage"}", IR1_N)
                mydriver.Write($"{"Part_Number["}{partpos}{"].IR_Zone2_Percentage"}", IR2_N)
                mydriver.Write($"{"Part_Number["}{partpos}{"].IR_Zone3_Percentage"}", IR3_N)
                mydriver.Write($"{"Part_Number["}{partpos}{"].IR_Zone4_Percentage"}", IR4_N)
                mydriver.Write($"{"Part_Number["}{partpos}{"].Name"}", PNAME_V)
                mydriver.Write($"{"Part_Number["}{partpos}{"].Part_Number"}", PNUMBER_V)
                partwrite = False
                Page2.ArrayLastPos.Value = partpos
                lastpos = partpos
           End If
    End if
    Is there a more simple way of doing this? Would this match out anything that contains a letter or something with only letters?
    Also if anyone could point me in the direction of a video/post/document that could explain the RegEx function a little better that would be great!

    Thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,155

    Re: Removing inputs that are only alphabetic values

    Learning RegEx is kind of mind numbing. Better might be just to bookmark a good tutorial for reference. I'm on the wrong computer to share the one I was using, but this one looks pretty good, and lets you try out different strings:

    https://regexone.com/lesson/letters_and_digits

    As long as you have your RegEx correct, then that approach would be a pretty reasonable way to go about it.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2024
    Posts
    11

    Re: Removing inputs that are only alphabetic values

    Quote Originally Posted by Shaggy Hiker View Post
    Learning RegEx is kind of mind numbing. Better might be just to bookmark a good tutorial for reference. I'm on the wrong computer to share the one I was using, but this one looks pretty good, and lets you try out different strings:

    https://regexone.com/lesson/letters_and_digits

    As long as you have your RegEx correct, then that approach would be a pretty reasonable way to go about it.
    Holy Smokes! This is beyond perfect, thank you!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,464

    Re: Removing inputs that are only alphabetic values

    For the record, Regex is a type - a class - not a function. In the code you posted, IsMatch is the function. Perhaps what you meant was "functionality", which is provided by the Regex class and, more generally, the types contained in the System.Text.RegularExpressions namespace.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2024
    Posts
    11

    Re: Removing inputs that are only alphabetic values

    Quote Originally Posted by jmcilhinney View Post
    For the record, Regex is a type - a class - not a function. In the code you posted, IsMatch is the function. Perhaps what you meant was "functionality", which is provided by the Regex class and, more generally, the types contained in the System.Text.RegularExpressions namespace.
    Does this mean I wouldn't be able to use it as I have it now? Currently I have it imported as
    Code:
    Imports System.Text.RegularExpressions
    , and yes functionality was what I was confused about but the lesson tutorial that Shaggy posted was very helpful.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,464

    Re: Removing inputs that are only alphabetic values

    You are importing a namespace, which simply mean that you can use types in that namespace unqualified. Without that, you'd have to use the fully-qualified name each time you used the Regex class in code. That's all that import does. It's useful for writing more succinct code but it has zero effect on the compiled code.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,803

    Re: Removing inputs that are only alphabetic values

    AI doesn't do many things extremely well, but RegEx is something that AI thrives at. I used to pride myself on building RegEx patterns but with AI it's basically a skill I don't really need anymore.

    For example, this is what I asked:
    Create a .NET RegEx pattern to match text that only contain alphabetical characters or whitespace.
    And this was its response:
    -------
    To create a regular expression in .NET that matches strings containing only alphabetical characters (both uppercase and lowercase) or whitespace, you can use the following pattern:
    Code:
    ^[a-zA-Z\s]+$
    Breakdown of the Pattern
    • `^`: Asserts the position at the start of the string.
    • `[a-zA-Z\s]`: Character class that matches:
      • `a-z`: Any lowercase alphabetic character.
      • `A-Z`: Any lowercase alphabetic character.
      • `\s`: Any whitespace character (includes spaces, tabs, line breaks).
    • `+`: Matches one or more of the preceding token (the character class [a-zA-Z\s]), ensuring that the string isn't empty.
    • `$`: Asserts the position at the end of the string.


    Considerations
    • This regex will allow strings that consist entirely of whitespace, e.g., " ". If you want at least one alphabetical character to be mandatory, you would need a slightly different approach.
    • The \s includes various types of whitespace. If you only want to match spaces and not other types of whitespace (like tabs or newlines), you could use a space character explicitly instead of \s.
    • The pattern ensures that the entire string matches the condition due to the use of anchors (^ and $). Without these, the regex could match a substring within a larger string that includes non-matching characters.


    ---

    Pretty thorough if you ask me.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,849

    Re: Removing inputs that are only alphabetic values

    Quote Originally Posted by Shaggy Hiker View Post
    Learning RegEx is kind of mind numbing. Better might be just to bookmark a good tutorial for reference. I'm on the wrong computer to share the one I was using, but this one looks pretty good, and lets you try out different strings:

    https://regexone.com/lesson/letters_and_digits

    As long as you have your RegEx correct, then that approach would be a pretty reasonable way to go about it.
    Not only is it "mind numbing" it sucks for people that are left behind to maintain it. I really hate when people that are really good at it use it all over the place. Then people that never once needed it for anything have to learn it after they are gone, or rework what they did.
    Last edited by TysonLPrice; Apr 17th, 2024 at 11:28 AM.
    Please remember next time...elections matter!

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Removing inputs that are only alphabetic values

    Quote Originally Posted by TysonLPrice View Post
    Not only is it "mind numbing" it sucks for people that are left behind to maintain it. I really hate when people that are really good at it use it all over the place. Then people that never once needed it for anything have to learn it after they are gone, or rework what they did.
    Professional programmers who might not always be around to maintain their software, should always comment their code thoroughly, particularly the more complex code, as some code can take a lot of headscratching to decipher when the responsibility of maintenance passes on to others.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,803

    Re: Removing inputs that are only alphabetic values

    Quote Originally Posted by .paul. View Post
    Professional programmers who might not always be around to maintain their software, should always comment their code thoroughly, particularly the more complex code, as some code can take a lot of headscratching to decipher when the responsibility of maintenance passes on to others.
    The architect I work for despises comments and will reject pull requests if they contain comments except on extremely rare occasions. Unfortunately complex RegEx patterns do not qualify as an "extremely rare occasion"
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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