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

Help needed for regexp 1

Status
Not open for further replies.

sbts1

Programmer
May 31, 2005
3
US
Please help to resolve a problem related to regular expression and matching everything except "&&".

I have the following script

set bw tu12-1-1-1-3&tu12-1-2-1&&-3&tu12-1-3-1&tu12-1-7-2&&-3
regexp {([^&&]*)} $bw match
puts $match -> the output is "tu12-1-1-1-3"
==========
I wanted to match everything excluding the first &&, so the result should have been "tu12-1-1-1-3&tu12-1-2-1", instead I am getting "tu12-1-1-1-3"

Please help.

Thanks
Sbts1
 
If you would to solve your problem with a single regular expression try:
regexp {^(.*?)&&} $bw dummy match

Easier to understand is the following two-line-version:
regexp -indices {&&} $bw indx
set match [ string range $bw 0 [ expr [ lindex $indx 0 ] - 1 ] ]

good luck !
 
Thanks esjo, I tried your code and it is working.
The first one in fact generated the string including the &&., but the second one worked fine.

Thanks again
sbts1
 
In the first solution you should use the value of "$match" and not that of "$dummy" ;-)

esjo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top