INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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

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

Try this

CODE

set line "number 108, then something other and at end number 108"
puts "\$line='$line'\n"

set result [regexp {\d+$} $line match]
puts "Matching Result=$result"
if {$result} {
  puts "This was matched: '$match'"
  regsub {\d+$} $line "" line
  puts "result after substitution:\n\$line='$line'\n"
} 
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 ' 

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close