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!

Help with string match?

Status
Not open for further replies.

alord

Technical User
May 11, 2012
3
0
0
US
Folks,
I am at a loss... Im trying to use 'string match' to compare 2 items in a list. However, the 2 items contain special characters. This all falls into a much larger script that I wrote but I wrote a small one here to show what I am doing as an example.

#!/usr/local/bin/expect

set MasterList

  • lappend MasterList {\"B\"}
    lappend MasterList {\"B\"}

    puts "MasterList=$MasterList"

    set value [lindex $MasterList 0]
    set value1 [lindex $MasterList 1]
    if {[string match $value $value1] != 1} {
    puts "They do not match"
    } else {
    puts "They match"
    }

    I'm just stuffing 2 strings(with special chars) into the list. Then pulling them out individually and comparing them. The 'string match' comparison is always returning a 0, saying they dont match....
    Any ideas as to how to compare strings with special chars like this?
    Thanks in advance for your help!
 
Hi

Why you want to compare them with [tt]string match[/tt] ?

Anyway. Backslashes ( \ ) have special meaning in the pattern but not in the string. So backslashes in the pattern must be escaped with backslashes :
Code:
[b]string[/b] match [teal]{[i]"B[/i]"}[/teal] [teal]{\"B\"}[/teal]

Feherke.
 
Feherke,
Thanks for replying... As I had mentioned, this is all part of a much bigger script. This other script reads in strings and shoves them into lists. These lists are then compared.... In these lists, i have alot of these special chars like \.... I need a way to compare these 2 strings in this manner. I dont particularly need to use string match.
 
Hi

Oops. Something gone wrong while posting. Intended to write :
Code:
[b]string match[/b] [teal]{\\"B\\"} {\"B\"}[/teal]

alord said:
I dont particularly need to use string match.
Ok. Then I suggest to use [tt]string equal[/tt] :
Code:
[b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] equal [navy]$value[/navy] [navy]$value1[/navy][teal]][/teal] [teal]!=[/teal] [purple]1[/purple][teal]}[/teal] [teal]{[/teal]


Feherke.
 
Feherke,
That was what I needed!!!! Thanks for your help! It's working now!
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top