Results 1 to 3 of 3

Thread: tcl regexp help needed

  1. #1
    Join Date
    Mar 2011
    Location
    Goshen, IN
    Beans
    396
    Distro
    Ubuntu 14.04 Trusty Tahr

    tcl regexp help needed

    I've been using tcl for awhile now. Nothing super fancy, mostly low-grade stuff. I'm always wanting to learn something new, and try things different ways.

    So, my question:

    suppose a variable 'test' that can be any value from 1-29.

    I want to check for it's value and steer my logic depending on it's value. I want to check if the value is one of the following:
    0, 1, 3, or 24.

    I accomplished this with:
    Code:
    switch $test {
         0 {do stuff}
         1 {do stuff}
         3 {do stuff}
         24 {do stuff}
         default {do other stuffs}
    }
    Problem solved...........

    But I want to learn regular expressions a bit more. So I tried:

    Code:
    if { [regexp {[0]|[1]|[3]|[24]} $test] == 1} {
         do stuff
    } else {
         do other stuffs
    }
    But my regex is not working. It matches a 2. I need it to match any one of those numbers (0, 1, 3, or 24) and the whole number only.

    What am I doing wrong?

  2. #2
    Join Date
    Feb 2006
    Location
    Vancouver, BC, Canada
    Beans
    318

    Re: tcl regexp help needed

    Code:
    if { [regexp {[0]|[1]|[3]|[24]} $test] == 1} {
         do stuff
    } else {
         do other stuffs
    }
    You are using character classes unnecessarily.
    The syntax [24] matches any one of the characters inside the [], ie. 2 or 4.
    That is why your code matches "2".

    Try eliminating square brackets from the regexp.

    You may also need word boundaries or line delimiters on either end of your regex, depending on what your input string looks like. For example, if your input string was "240" then the regex {1|3|24} would match it unless you added something to the end of the regex to tell it to stop matching after the 4.
    Last edited by stylishpants; November 6th, 2012 at 11:56 PM.

  3. #3
    Join Date
    Mar 2011
    Location
    Goshen, IN
    Beans
    396
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: tcl regexp help needed

    Yes, that worked. Thank you!

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
  •