![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl RegExp to remove last character from strings | ospreyeagle | Shell Programming and Scripting | 2 | 04-03-2008 02:55 PM |
| Matching character | felixwhoals | UNIX for Dummies Questions & Answers | 2 | 01-09-2008 07:11 PM |
| GnuWin32 sed 4.1.4 regexp matching | Simerian | UNIX for Dummies Questions & Answers | 2 | 06-23-2005 12:28 AM |
| special character ? | mile1982 | High Level Programming | 1 | 10-19-2004 08:15 AM |
| character matching | Rukshan | UNIX for Dummies Questions & Answers | 1 | 09-19-2000 09:03 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
tcl: regexp matching special character
Hello Experts,
Can someone help me here: I have a variable which contains a string with "[]". set var1 {a[1]} set str1 {a[1] is the element i want to match} Now "regexp $var1 $str1" does not work? ("regexp {a\[1\]} $str1" works, but var1 gets it's value automatically from another script) Is there a way to make it work? (adding backslash "\" to var1 might not be feasible) |
|
||||
|
doesn't look right to me
Can you explain 'How it works?'?
It doesn't seem right to me. % set var1 {a[1]} a[1] % set str1 {a[1] is the element i want to match} a[1] is the element i want to match % regexp $var1 $str1 0 % regexp {[$var1]} $str1 1 % set var1 {a[10]} a[10] % regexp {[$var1]} $str1 1 % set var1 {a[9080]} a[9080] % regexp {[$var1]} $str1 1 |
|
||||
|
If you want a substring exact match that doesn't really rely on regexp syntax look at string match instead, but ymmv. I'm having trouble obtaining a sane result.You can always brute force the search..something like.
Code:
proc paramSearch {pattern str} {
set t [string length $pattern]
for {set i 0} {$i < [string length $str]} {incr i $t} {
if {[string compare $pattern [string range $str $i [expr $i + [ expr $t - 1]]]] == 0} {return 1; #puts "Match at string index: $i"}
#puts "Compared $pattern and [string range $str $i [expr $i + [ expr $t - 1]]]"
}
}
() 92 % set vv
thisis[]a[1]test
() 93 % set pat
a[1]
() 94 % paramSearch $pat $vv
1
|
|
||||
|
Off by one...
Code:
proc paramSearch {pattern str} {
set t [expr [string length $pattern] - 1]
for {set i 0} {$i < [string length $str]} {incr i $t} {
if {[string compare $pattern [string range $str $i [expr $i + $t]]] == 0} {return 1; #puts "Match at string index: $i"}
#puts "Compared $pattern and [string range $str $i [expr $i + $t]]"
}
}
|
|
||||
|
i would rather use lsearch
i was trying to understand if (and how) that can be done with 'regexp'?
Otherwise, lsearch -exact $str1 $var1 does the job pretty well. % set var1 {a[1]} a[1] % set var2 {aa[1]} aa[1] % set var3 {a[11]} a[11] % set str1 {a[1] is the element we are looking for and not aa[1] or a[11]} a[1] is the element we are looking for and not aa[1] or a[11] % lsearch -exact $str1 $var1 0 % lsearch -exact $str1 $var2 10 % lsearch -exact $str1 $var3 12 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|