Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble to replace a number at the end of the line by empty string 2

Status
Not open for further replies.

tpham1002002

Technical User
Jun 20, 2012
6
0
0
US
Hi All,
I have trouble to replace the number at the end of this line by empty string, for example I want to replace number 108 (notice that this number repeats many times in the line) at the end of this line by empty string
report_timing -from \[get_cells \"uart_rx_i0/uart_108rx_ctl_i0/*\" -filter {IS_SEQUENTIAL}\] -to \[get_cells \"uart_rx_i0/uart_108rx_ctl_i0/*\" -filter {IS_SEQUENTIAL}\] 108

Here is my code

1) I put the line to a variable
set me "report_timing -from \[get_cells \"uart_rx_i0/uart_108rx_ctl_i0/*\" -filter {IS_SEQUENTIAL}\] -to \[get_cells \"uart_rx_i0/uart_108rx_ctl_i0/*\" -filter {IS_SEQUENTIAL}\] 108"

Get the number at the end of the line

set d [regexp {([0-9]+$)} $me p q]

but I dont know how to replace it by empty string
I tried

regsub {([0-9]+$)} $me "" me

but it doesn't work.
I would like to have your help to resolve the issue
Regards
Tony
 
Try this
Code:
[COLOR=#804040][b]set[/b][/color] line [COLOR=#ff00ff]"number 108, then something other and at end number 108"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]line='$line'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {\d+$} [COLOR=#008080]$line[/color] match]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Matching Result=$result"[/color]
[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$result[/color]} {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"This was matched: '$match'"[/color]
  [COLOR=#804040][b]regsub[/b][/color] {\d+$} [COLOR=#008080]$line[/color] [COLOR=#ff00ff]""[/color] line
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"result after substitution:[/color][COLOR=#6a5acd]\n\$[/color][COLOR=#ff00ff]line='$line'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
}
Output:
Code:
$line='number 108, then something other and at end number 108'

Matching Result=1
This was matched: '108'
result after substitution:
$line='number 108, then something other and at end number '
 
Hi,

Not 100% sure if this is what u want, but here my shot:

Code:
set me [string range $me 0 [string last " " $me]]

Mind though, this method does assume a certain condition to the content of variable $me: the original $me content needs to have a space character before the actual number/string you would like to replace with an empty string and it only removes the string at the end of the line, the number 108 in your case. The trailing space character is not removed, as you prefer.

As you can see, there are several ways to solve your problem. If you like regular expressions and/or being dynamic, go for mikrom's solution.

thacoda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top