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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search command 1

Status
Not open for further replies.

bebig

Technical User
Oct 21, 2004
111
US
I am trying to search a string and replace it.
I can read a file, but I do not know how to search exact word and replace to another value.
Could you do me a favor?
thank you in advance.

set openfile open ["xmlpage.xml" "r"]
gets $openfile line
while {![eof $openfile]} {

#---this part, I think I need search command.
puts $line
gets $openfile line
}
}
 
Thank you so much..I got correct answer..

regexp {(\<edgetime\>)([0-9]*:[0-9]*)} $line m1 m2 m3

set a 09:20

regsub {\<edgetime\>[0-9]*:[0-9]*} $line "\<edgetime\>$" line
 
because you never [red]split[/red] m3 into a list and then operated on the [red]elements[/red] of the list. Anyway, as I said before, if your substitution is [red]independent[/red] of the prior value, you derive no benefit from extracting it (with regexp) before substituting. If all you want to do is replace whatever is in between the tags with, say, 09:20, then one pass of regsub will do the trick:
Code:
set openfile open ["xmlpage.xml" "r"]
[red]set a 09:20[/red]
while {![eof $openfile]} {
  gets $openfile line
  [red]regsub {\<edgetime\>[0-9]*:[0-9]*} $line "\<edgetime\>$a" line[/red]
  puts $line

}

Bob Rashkin
rrashkin@csc.com
 
oh..I have one more question.
what if

<stateid>1</stateid>

regexp {(\<stateid\>)[0-9]*} $line s1 s2 s3


puts $1

but I got error..I think...[0-9]* is wrong.
would you please tell me where I did wrong??
thank you
 
I see a couple of things that may or may not be just typos:

set line <stateid>1</stateid>
regexp {(\<stateid\>)[0-9]*} $line s1 s2 s3
puts $1


First, "1" isn't a declared variable so "$1" is not going to mean anything. Of course, naming a variable with a number is a very bad idea so I think that's a typo.
Let's look at the "regexp" syntax (and pardon me if this is stuff you know). The "[0-9]*" means "any digit between 0 and 9 any number of times". It's not the part that's wrong. If you take your regexp statement the way it is, it says "find, in 'line', the string, '<stateid>' followed by any number of digits". It also says "call <stateid> a group". It also says "put the entire match in s1, put the first group in s2, and put the 2nd group in s3". But you haven't defined a second group. If you look at the values of s1, s2, and s3:
s1 = <stateid>1
s2 = <stateid>
s3 = --nothing--

If you put parentheses around the "[0-9]*" in your regular expression, you make it a second group:
regexp {(\<stateid\>)([0-9]*)} $line s1 s2 s3

now s3 = 1

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top