Forum Discussion

mazek_59373's avatar
mazek_59373
Icon for Nimbostratus rankNimbostratus
Mar 02, 2009

regexp

Hi,

 

 

I'm trying to match via a simple regexp an URI. It can be in a form of:

 

 

/sth/digits

 

/sth/digits.digit

 

/sth/digits_digit

 

 

 

example URI to be matched is: /aaaa/999.8

 

 

I would like to get only "999" of it. I'm trying to use regexp below:

 

 

set matched [regexp {([0-9]+)[\._]{0,1}[0-9]{0,1}$} $url id]

 

 

 

but id is still matched as a 999.8?

 

 

 

Regexp is quite simple, but maybe I misunderstand something. Any hints?

 

 

 

br

 

 

mazek

1 Reply

  • Hi Mazek,

    Maybe something like this?

     
     % regexp {[/\w]*/(\d+)[._\d]*} /letters/123456_789 unused id 
     1 
     % puts $id 
     123456 
      
     % regexp {[/\w]*/(\d+)[._\d]*} /letters/123456.789 unused id 
     1 
     % puts $id 
     123456 
      
     % regexp {[/\w]*/(\d+)[._\d]*} /letters/123456 unused id 
     1 
     % puts $id 
     123456 
      
     % regexp {[/\w]*/(\d+)[._\d]*} /123456 unused id 
      
     

    In case the regex gets mangled on the forums, try this with the spaces removed:

    regexp { [ / \ w ] * / ( \ d + ) [ . _ \ d ] * } $uri unused id

    $id will have the value you're looking for.

    Aaron