Forum Discussion

paul_73820's avatar
paul_73820
Icon for Nimbostratus rankNimbostratus
Nov 07, 2012

regex for a number between 1-99

Hi There,

 

I am trying to validate a string with that is like this:

 

pw-10:1000.100

 

The regex i am using is:

 

^pw-[1-9]\d?

 

This works in perl, and infact in tcl 8.5 but doesnt match in the irule for some reason.

 

the tcl i am using to see if it work is:

 

set string "12 Again and again and again ..."

 

if { [regexp {(^[1-9]\d?)} $string => digits] } {

 

puts "we got $digits in string"

 

} else {

 

puts "nothing here"

 

}

 

 

The irule is like this:

 

 

switch -regex $string {

 

"^pw-[1-9]\d?:" {

 

do some stuff

 

}

 

default {

 

}

 

 

There is more of course but you get the idea.

 

 

Any ideas?

 

 

Paul

 

 

 

3 Replies

  • I think i see the issue.

     

    i wasnt separating ?: so it fails.

     

    Doing ^pw-([1-9]\d?): works fine.

     

     

  • Hi Paul,

    If you can use a string or scan command it should be more efficient than regexp:

    
    switch -glog $str {
    pw-[1-9]* -
    }
    

    Note that I didn't check for the optional digit following 1-9 as you had it listed as optional in your regex.

    Aaron
  • Thanks hoolio,

     

     

    I think you mean glob ;)

     

    If you use glob is it a string match?

     

    I was using regex as we had a number of tricky strings that were evaluated in the same switch, thats not the case now so might be time to review and drop the regex.

     

     

    I havent noticed any real impact due to regex yet.

     

     

    Paul