Angie
Angie

Reputation: 25

parse a line with special characters, digits, spaces and get a part of it using regexp in tcl

I need to parse lines which looks like:

1.3.6.1.4.1.2076.50.3.1.3.2   2  
1.3.6.1.4.1.2076.50.3.1.4.1   64      127.0.0.101

I use the below code to parse it:

while {[gets $in line] != -1} {
    
     regexp {(.*\.)\d+} $line -> line 
     puts "$line \n" }

in this case i get

1.3.6.1.4.1.2076.50.3.1.3.       ---> I don't want to hold the "." at the end
1.3.6.1.4.1.2076.50.3.1.4.1   64      127.0.0. -----> it holds also the whole string with values    and remove only the last dgits after the last dot.

how can modify the regexp so as to take the below reults:

1.3.6.1.4.1.2076.50.3.1.3 
1.3.6.1.4.1.2076.50.3.1.4

Upvotes: 1

Views: 62

Answers (2)

Shawn
Shawn

Reputation: 52384

A non-regexp version for comparison:

while {[gets $in line] >= 0} {
    set word [lindex [split $line] 0]
    puts [string range $word 0 [string last . $word]-1]
}

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

You can use

regexp {^(\S+)\.\d+} $line -> line 

See the Tcl demo online.

The regex matches

  • ^ - start of string
  • (\S+) - Group 1 (its contents are assigned to line): one or more non-whitespace chars
  • \. - a literal dot
  • \d+ - one or more digits.

See the regex demo.

Upvotes: 3

Related Questions